求助:关于图形用户界面
下面是我写的两段代码:前段中的一个JComboBox是私有的而后段我把它改成了共有的为什么结果差距那么大,前者能选中,而后者一直都是默认值不变呢?import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
class SetPanel1 extends JPanel
{
final String text[] = {"红","黄","绿","蓝","黑","白"};
private static JTextField jtf = new JTextField("选择颜色:",5);
private static JTextField jtf1 = new JTextField("当前的前景色是蓝色,背景色是黄色");
private JComboBox jcb = new JComboBox(text);
private Color background;
public SetPanel1(Color backgroundcolor)
{
setBorder(BorderFactory.createEmptyBorder());
setSize(600,40);
jtf.setEditable(false);
add(jtf);
jcb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int i;
i = jcb.getSelectedIndex();
switch(i)
{
case 0:
background = Color.RED;
System.out.print(i);
System.out.print(background);
break;
case 1:
background = Color.YELLOW;
System.out.print(i);
System.out.print(background);
break;
case 2:
background = Color.GREEN;
System.out.print(i);
System.out.print(background);
setBackground(background);
break;
case 3:
background = Color.BLUE;
System.out.print(i);
System.out.print(background);
setBackground(background);
break;
case 4:
background = Color.BLACK;
System.out.print(i);
System.out.print(background);
setBackground(background);
break;
case 5:
background = Color.WHITE;
System.out.print(i);
System.out.print(background);
setBackground(background);
break;
default:
break;
}
}
});
add(jcb);
add(jtf1);
}
}
class SetPanel2 extends JPanel
{
JRadioButton jrb = new JRadioButton("背景色");
JRadioButton jrb1 = new JRadioButton("前景色");
ButtonGroup bg = new ButtonGroup();
JTextField jtf = new JTextField("颜色设置:",5);
public SetPanel2()
{
setBorder(BorderFactory.createEmptyBorder());
setSize(new Dimension(20,340));
setLayout(new GridLayout(10,1));
jtf.setEditable(false);
add(jtf);
bg.add(jrb);
bg.add(jrb1);
add(jrb1);
add(jrb);
}
}
class SetPanel3 extends JPanel
{
JTextArea jta = new JTextArea(10,40);
public SetPanel3()
{
setBorder(BorderFactory.createTitledBorder("请输入文字:"));
setSize(580,340);
add(new JScrollPane(jta));
}
}
class SetPanel4 extends JPanel
{
JButton jb = new JButton("确定");
public SetPanel4()
{
setBorder(BorderFactory.createEmptyBorder());
setSize(600,20);
add(jb);
}
}
public class J_Exercise
{
public static Color backgroundcolor = Color.YELLOW;
public static Color forecolor = Color.RED;
public static void main(String args[])
{
JFrame frame = new JFrame("彩色记事板");
Container container = frame.getContentPane();
container.setLayout(new BorderLayout());
SetPanel1 sp1 = new SetPanel1(backgroundcolor);
SetPanel2 sp2 = new SetPanel2();
SetPanel3 sp3 = new SetPanel3();
sp3.setBackground(backgroundcolor);
sp3.setForeground(forecolor);
SetPanel4 sp4 = new SetPanel4();
frame.add(sp1,BorderLayout.NORTH);
frame.add(sp2,BorderLayout.WEST);
frame.add(sp3,BorderLayout.CENTER);
frame.add(sp4,BorderLayout.SOUTH);
frame.setSize(600,400);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
class Data
{
public static JPanel panel[] = { new JPanel(),new JPanel(new GridLayout(10,1)),
new JPanel(),new JPanel()};
final String text[] = {"红","黄","绿","蓝","黑","白"};
protected static JTextField jtf1 = new JTextField("选择颜色:",5);
protected static JTextField jtf2 = new JTextField("当前的前景色是蓝色,背景色是黄色");
protected static JTextField jtf3 = new JTextField("颜色设置:",5);
protected JComboBox jcb = new JComboBox(text);
protected static JRadioButton jrb1 = new JRadioButton("背景色");
protected static JRadioButton jrb2 = new JRadioButton("前景色");
protected static ButtonGroup bg = new ButtonGroup();
protected static JTextArea jta = new JTextArea(10,40);
protected static JButton jb = new JButton("确定");
protected static Color background = Color.BLACK;
protected static Color forecolor = Color.BLUE;
}
class Set extends Data
{
public Set()
{
panel[0].setSize(600,40);
panel[0].setBorder(BorderFactory.createEmptyBorder());
panel[1].setSize(20,340);
panel[1].setBorder(BorderFactory.createEmptyBorder());
panel[2].setSize(580,340);
panel[2].setBorder(BorderFactory.createTitledBorder("请输入文字:"));
panel[3].setSize(600,20);
panel[3].setBorder(BorderFactory.createEmptyBorder());
bg.add(jrb1);
bg.add(jrb2);
panel[0].add(jtf1);
panel[0].add(jcb);
panel[0].add(jtf2);
panel[1].add(jtf3);
panel[1].add(jrb1);
panel[1].add(jrb2);
panel[2].add(new JScrollPane(jta));
panel[3].add(jb);
}
public void Setpanel1()
{
JcbAction handler = new JcbAction();
jcb.addActionListener(handler);
}
public void Setpanel4()
{
JbAction handler = new JbAction();
jb.addActionListener(handler);
}
}
class JbAction extends Data implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
panel[2].setBackground(background);
jta.setBackground(forecolor);
}
}
class JcbAction extends Data implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int i;
i = jcb.getSelectedIndex();
switch(i)
{
case 0:
background = Color.RED;
System.out.print(background);
break;
case 1:
background = Color.YELLOW;
System.out.print(background);
break;
case 2:
background = Color.GREEN;
System.out.print(background);
break;
case 3:
background = Color.BLUE;
System.out.print(background);
break;
case 4:
background = Color.BLACK;
System.out.print(background);
break;
case 5:
background = Color.WHITE;
System.out.print(background);
break;
default:
break;
}
}
}
public class exercise extends Data
{
public static void main(String args[])
{
JFrame frame = new JFrame("彩色面板");
Set set = new Set();
Container container = frame.getContentPane();
container.setLayout(new BorderLayout());
container.add(set.panel[0],BorderLayout.NORTH);
container.add(set.panel[1],BorderLayout.WEST);
container.add(set.panel[2],BorderLayout.CENTER);
container.add(set.panel[3],BorderLayout.SOUTH);
frame.setSize(600,400);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
set.Setpanel1();
set.Setpanel4();
}
}