我已经制作了一个程序,我在其中comboBox
然后添加actionlistener
,如果我点击它会出现一个框架。
代码:
String menulist [] = {"", "Array", "BubbleSort" ," - Selection Sort"," - Insertion Sort"," - Merge Sort","Recursion"," - Factorial"," - Anagramming"," - Towers of Hanoi","Stacks","Trees"," - Types of Trees","Binary Tree","Shell Sort","Quick Sort","Hash Table","Graphs"};
final JComboBox SelectMenu = new JComboBox(menulist);
SelectMenu.setFont(new Font("Tahoma", Font.PLAIN, 17));
SelectMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int list = SelectMenu.getSelectedIndex();
if(list == 1){
//Array
JFrame frameArray = new JFrame();
frameArray.setBounds(100, 100, 830, 601);
frameArray.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frameArray.getContentPane().setLayout(null);
frameArray.setVisible(true);
JLabel lblArray = new JLabel("Array");
lblArray.setFont(new Font("Lucida Bright", Font.BOLD | Font.ITALIC, 25));
lblArray.setBounds(368, 63, 122, 41);
frameArray.getContentPane().add(lblArray);
JLabel AnArrayIs = new JLabel();
AnArrayIs.setBackground(Color.WHITE);
AnArrayIs.setText("An array is a systematic arrangement of similar objects, usually in rows and columns.");
AnArrayIs.setBounds(46, 103, 357, 432);
frameArray.getContentPane().add(AnArrayIs);
我会使用if-else,在这种情况下,如果将近16个示例,我会使用很多。有没有办法可以为框架编写更短的代码?所以我会继续做if-else直到16指数?
提前谢谢!! (:
答案 0 :(得分:1)
如果您要更改的唯一内容是标签和文本,那么请创建一个类似的方法:
public Jframe getJframe(String label, String text){
JFrame frameArray = new JFrame();
frameArray.setBounds(100, 100, 830, 601);
frameArray.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frameArray.getContentPane().setLayout(null);
frameArray.setVisible(true);
JLabel lblArray = new JLabel(label);
lblArray.setFont(new Font("Lucida Bright", Font.BOLD | Font.ITALIC, 25));
lblArray.setBounds(368, 63, 122, 41);
frameArray.getContentPane().add(lblArray);
JLabel AnArrayIs = new JLabel();
AnArrayIs.setBackground(Color.WHITE);
AnArrayIs.setText(text);
AnArrayIs.setBounds(46, 103, 357, 432);
frameArray.getContentPane().add(AnArrayIs);
return frameArray;
}
那么你的if语句或switch语句只需要看起来像:
Jframe myJframe = null;
if(list==1){
myJframe = getJframe(menuList[1],"An array is.....")
} else if(list ==2){
myJframe = getJframe(menuList[2],"BubbleSort is ...")
}....
我可能会为定义创建常量,以便您可以轻松更改它们,例如:
public static final String ARRAY_DEFINITION = "An array is a ....";
答案 1 :(得分:1)
使代码不那么复杂的一种方法是创建不同的JFrame
子类来处理每个索引。
第一个索引是“Array”,因此创建一个新的JFrame
子类:
public class ArrayFrame extends JFrame {
public ArrayFrame () {
setBounds(100, 100, 830, 601);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
JLabel lbl = new JLabel("Array");
lbl.setFont(new Font("Lucida Bright", Font.BOLD | Font.ITALIC, 25));
lbl.setBounds(368, 63, 122, 41);
getContentPane().add(lbl);
JLabel detailLbl = new JLabel();
detailLbl.setBackground(Color.WHITE);
detailLbl.setText("An array is...");
detailLbl.setBounds(46, 103, 357, 432);
getContentPane().add(detailLbl);
}
}
在if语句中,创建ArrayFrame
的实例并打开框架:
if (list == 1) {
ArrayFrame frame = new ArrayFrame();
frame.setVisible(true);
}
这更易于维护并隐藏了许多细节,这意味着它更抽象。每个人都喜欢OOP中的抽象!
“好吧,我仍然需要创建16个课程,”你说。你实际上可以创建一个类!
public class MyFrame extends JFrame {
public MyFrame (String title, String details) {
setBounds(100, 100, 830, 601);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
JLabel lbl = new JLabel(title);
lbl.setFont(new Font("Lucida Bright", Font.BOLD | Font.ITALIC, 25));
lbl.setBounds(368, 63, 122, 41);
getContentPane().add(lbl);
JLabel detailLbl = new JLabel();
detailLbl.setBackground(Color.WHITE);
detailLbl.setText(details);
detailLbl.setBounds(46, 103, 357, 432);
getContentPane().add(detailLbl);
}
}
你可以通过调用这样的构造函数来创建这个框架:
if (list == 1) {
MyFrame frame = new MyFrame ("Array", "An array is...");
frame.setVisible(true);
}
另外,请考虑使用开关而不是if语句。
答案 2 :(得分:0)
我会使用if-else,在这种情况下,如果将近16个示例,我会使用很多。有没有办法可以为框架编写更短的代码?所以我会继续做if-else直到16指数?
我认为您最好的选择是使用switch
语句而不是16 if / else语句。它会简单得多。
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}