【发布时间】:2015-09-20 23:50:57
【问题描述】:
我在这里遇到了两个问题。我正在制作一个简单的程序来测试,基本上当你点击按钮JColorChooser 时会弹出,你可以选择你想要的矩形是什么颜色。第二个问题是我无法将按钮定位在BorderLayout.SOUTH 或BorderLayout.NORTH 或任何地方。这些是我的代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI extends JPanel {
private Color a = (Color.WHITE);
private Color b = (Color.WHITE);
private final JPanel panel;
private final JButton ab;
private final JButton bb;
private int x = 1;
private int y = 1;
public GUI() {
panel = new JPanel();
panel.setBackground(Color.WHITE);
ab = new JButton("Choose your first Rectangle color");
ab.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
a = JColorChooser.showDialog(null, "Pick a Color", a);
x = 2;
}
});
bb = new JButton("Choose your second Rectangle color");
bb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
b = JColorChooser.showDialog(null, "Pick a Color", b);
y = 2;
}
});
add(ab, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
add(bb, BorderLayout.SOUTH);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
if (x == 2)
g.setColor(a);
g.fillRect(50, 50, 100, 20);
if (y == 2)
g.setColor(b);
g.fillRect(50, 200, 100, 20);
}
}
【问题讨论】:
标签: java swing jbutton jcolorchooser