spiderman200700
دوشنبه 28 شهریور 1390, 02:26 صبح
وقت بخیر دوستان.
چطور میتونم JSeparator عمودی به یه JPopupMenu اضافه کنم.مثل این:
75474
ممنون. :لبخندساده:
saeedIRHA
دوشنبه 28 شهریور 1390, 04:38 صبح
برای اینکار باید LookAndFeelِ فریم ت رو عوض کنی، اینطوری:
public NewJFrame() {
LookAndFeel lf = UIManager.getLookAndFeel();
// Install a different look and feel; specifically, the Windows look and feel
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (InstantiationException e) {
} catch (ClassNotFoundException e) {
} catch (UnsupportedLookAndFeelException e) {
} catch (IllegalAccessException e) {
}
initComponents();
ادامهٔ کدت ......
برای اطلاعات بیشتر به اینجا مراجعه کن:
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
spiderman200700
دوشنبه 28 شهریور 1390, 18:10 عصر
ممنون از کمک های همیشگی شما.
ببخشید من خوب متوجه نشدم.
LookAndFeel فریم چه ربطی به JSeparator داره؟
میشه بیشتر توضیح بدید.
اگه لطف کنید یه مثال خیلی کوچولو از یه JPopupMenu که فقط یک آیتم داشته باشه و یه JSeparator عمودی داخلش add شده باشه و یه کلید میانبر ترکیبی هم داشته باشه واسم اینجا بذارید،خیلی خیلی ممنونتون میشم.
با تشکر.
saeedIRHA
دوشنبه 28 شهریور 1390, 20:51 عصر
JSeperator رو وقتی L&F به صورت Swing هست نمیتونی عمودی اضافه کنی (تا اونجا که من میدونم)
وقتی L&F رو به حالت پیشفرض windows عوض کنی اون قسمت که شکل JSeperator عمودی هست اضافه میشه
البته توجه داشته باش که اون فقط یک خط هست برای جدا کردن iconها و هر خطی لزوما JSeperator نیست و خواصش رو هم نداره
import java.awt.Adjustable;
import java.awt.Point;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JViewport;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class NewJFrame extends javax.swing.JFrame {
protected Point mypoint = null;
/** Creates new form NewJFrame */
public NewJFrame() {
LookAndFeel lf = UIManager.getLookAndFeel();
// Install a different look and feel; specifically, the Windows look and feel
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (InstantiationException e) {
} catch (ClassNotFoundException e) {
} catch (UnsupportedLookAndFeelException e) {
} catch (IllegalAccessException e) {
}
initComponents();
jList1.setSelectedIndex(19);
String str = jList1.getSelectedValue().toString();
jLabel1.setText("item is: " + str + " --With Index number: " + jList1.getSelectedIndex());
JViewport myView = jScrollPane1.getViewport();
jList1.ensureIndexIsVisible(jList1.getSelectedInde x() -1);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPopupMenu1 = new javax.swing.JPopupMenu();
jMenu3 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jLabel1 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JPopupMenu.Separator();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenu3.setText("Popup Menu");
jMenuItem3.setText("Saeed");
jMenu3.add(jMenuItem3);
jMenuItem4.setText("ghiassy");
jMenu3.add(jMenuItem4);
jPopupMenu1.add(jMenu3);
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "one", "two", "three", "four", "one", "two", "three", "fourone", "two", "three", "fourone", "two", "three", "fourone", "two", "three", "fourone", "two", "three", "fourone", "two", "three", "four" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jList1.setComponentPopupMenu(jPopupMenu1);
jList1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jList1MouseClicked(evt);
}
});
jList1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
jList1MouseMoved(evt);
}
});
jScrollPane1.setViewportView(jList1);
jLabel1.setHorizontalAlignment(javax.swing.SwingCo nstants.CENTER);
jLabel1.setText("Nothing ");
jLabel1.setBorder(javax.swing.BorderFactory.create TitledBorder("Selected Item is:"));
jLabel1.setComponentPopupMenu(jPopupMenu1);
jMenu1.setText("File");
jMenuItem1.setText("New");
jMenu1.add(jMenuItem1);
jSeparator1.setOrientation(javax.swing.SwingConsta nts.VERTICAL);
jMenu1.add(jSeparator1);
jMenuItem2.setText("Exit");
jMenu1.add(jMenuItem2);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(66, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jList1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String str = jList1.getSelectedValue().toString();
jLabel1.setText("item is: " + str + " --With Index number: " + jList1.getSelectedIndex());
}
private void jList1MouseMoved(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
mypoint = new Point(evt.getX(),evt.getY());
System.out.println("X = " + mypoint.getX() + " Y = " + mypoint.getY());
//Point mypoint2 = null;
int i = jList1.locationToIndex(mypoint);
System.out.println("index is: "+ i);
jList1.setSelectedIndex(i);
String str = jList1.getSelectedValue().toString();
jLabel1.setText("item is: " + str + " --With Index number: " + jList1.getSelectedIndex());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JList jList1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JPopupMenu jPopupMenu1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPopupMenu.Separator jSeparator1;
// End of variables declaration
}
spiderman200700
دوشنبه 28 شهریور 1390, 22:06 عصر
ممنون از توضیحتون.
تشکر فراوان از شما. :تشویق:
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.