标题:求助:关于图形用户界面
只看楼主
herendagao
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-5-3
结帖率:100%
 问题点数:0 回复次数:1 
求助:关于图形用户界面
下面是我写的两段代码:前段中的一个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();
}
}
搜索更多相关主题的帖子: 图形 private import 背景色 用户 
2007-04-01 15:12
zhanglinkai
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2006-11-25
得分:0 
这个好象很乱啊,JComboBox在这里应该用addItemListener()
然后你创建个Color[]数组,用这个方法
background=Color[jbc.getSelectedIndex()];好象更好,
我也很菜,给个建议,待高手解答。

希望自己能走的更远!!!
2007-04-01 16:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-128511-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.446858 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved