【发布时间】:2015-01-23 20:19:36
【问题描述】:
我有一个名为“Panel”的类,它扩展了 JPanel,它位于另一个名为“Main”的类中。构造函数实例化 JFrame 和所有 GUI 组件,并将其全部设置好,例如大小。
扩展 JPanel 的“Panel”类有一个方法 public void paintComponent(Graphics g){},我在其中添加了一些 JButton 并使用了 g.drawString。
然后在“Main”类中,我将“Panel”添加到 JFrame。
我的问题是,我正在尝试为添加到“面板”类中的按钮实现 actionListener。 actionListener 函数将添加更多按钮并使用 g.drawString。现在我将在哪里放置 ActionListener 以便这样做?如何将 g.drawString 用于特定面板,并且 g.drawString 线位于另一个类中,即 ActionListener 类?我需要在 actionPerformed 中使用 PaintComponent 的 Graphics g。
谢谢!
编辑 - 代码示例:
public class Main{
private JFrame jf;
private JTextField jtf1;
private JTextField jtf2;
private Panel p;
private JComboBox jcb1;
private JComboBox jcb2;
private JButton button;
private Object options[];
//ActionListener Variables
private int string1 = 150;
private int string2 = 150;
private int yJtf1 = 150;
private int yJtf2 = 160;
private int cb1 = 140;
private int cb2 = 165;
private int count = 0;
public Main(){
jf= new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(700, 700);
p = new Panel();
jtf1 = new JTextField("", 20);
jtf2= new JTextField("", 20);
Object options[] = {""};
jcb1 = new JComboBox(tools);
jcb2 = new JComboBox(tools);
button = new JButton("+");
jf.add(p);
jf.setVisible(true);`
}
public class Panel extends JPanel{
public Panel(){
this.setLayout(null);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
/*button.addActionListener(new ActionListener(){ //Would this work or should the ActionListener be a class as shown below?
public void actionPerformed(ActionEvent e){
if(count < 3){ //Won't be allowed to add anymore after 3 times
string1 += 50;
string2 += 50;
jtf1 += 50;
jtf2 += 50;
cb1 += 50;
cb2 += 45;
//Would like to add the following components to the 'Panel' (which is a JPanel) whenever the JButton 'button' already added to 'Panel' is clicked.
p.add(jtf1); //Would doing p.add really add to the panel when the ActionListener is called?
jtf1.setBounds(60, yJtf1, 50, 40);
p.add(jtf2);
jtf2.setBounds(60, yJtf2, 50, 40);
add(jcb1);
jcb1.setBounds(250, cb1, 50, 40);
add(left2);
jcb2.setBounds(250, cb2, 50, 40);
Font font = new Font("TimesRoman", Font.BOLD, 18);
g.setFont(font); //Getting error on 'g' regardless
g.drawString("Hi", 15, string1); //This is the main problem, how would I be able to add this strings to the 'Panel' (which is a JPanel)
g.drawString("There", 330, string1);
}
count++;
}
});*/
add(jtf1);
jtf1.setBounds(100, 30, 120, 30);
add(jtf2);
ljtf2.setBounds(100, 60, 120, 30);
add(button);
plusButton.setBounds(200,150, 50, 50);
//button.addActionListener(new ButtonClicked()); if doing ActionListener via class like below
add(jcb1);
jcb1.setBounds(300, 350, 100, 50);
add(ljcb2);
jcb2.setBounds(300, 350, 100, 25);
Font font = new Font("Arial", Font.BOLD, 12);
g.setFont(font);
g.drawString("Item:", 40, 45);
g.drawString("Cost:", 40, 75);
}
}
public static void main(String [] args){
new Main();
}
class ButtonClicked implements ActionListener{ //Action Listener: The follow is what I am trying to implement
public void actionPerformed(ActionEvent ae){
if(count < 3){ //Won't be allowed to add anymore after 3 times
string1 += 50;
string2 += 50;
jtf1 += 50;
jtf2 += 50;
cb1 += 50;
cb2 += 45;
//Would like to add the following components to the 'Panel' (which is a JPanel) whenever the JButton 'button' already added to 'Panel' is clicked.
p.add(jtf1); //Would doing p.add really add to the panel when the ActionListener is called?
jtf1.setBounds(60, yJtf1, 50, 40);
p.add(jtf2);
jtf2.setBounds(60, yJtf2, 50, 40);
mp.add(jcb1);
jcb1.setBounds(250, cb1, 50, 40);
mp.add(left2);
jcb2.setBounds(250, cb2, 50, 40);
Font font = new Font("TimesRoman", Font.BOLD, 18);
g.setFont(font);
g.drawString("Hi", 15, string1); //This is the main problem, how would I be able to add this strings to the 'Panel' (which is a JPanel)
g.drawString("There", 330, string1);
}
count++;
}
}
}
【问题讨论】:
-
抱歉,我无法编译您的代码。这里面的问题太多了。但是要在您的代码中尽快回答您的问题,不,它不会起作用。你的代码没有意义。
paintComponent仅用于绘画(不添加 ActionListener 或更改组件状态)。顺便说一句,停止使用绝对定位并开始使用 LayoutManager:它会让您的生活更轻松。 -
@GuillaumePolet 这只是完整代码的 sn-p。那么我将如何去做,例如每当单击添加在“Panel”类中的“按钮”时,将 g.drawString 作为特定 JPanel 的 ActionListener?
-
这只是完整代码的一个 sn-p 我猜是这样,但即便如此,您的代码中也存在拼写错误,而且要解决的问题太多了可编译。如果你真的想使用drawString,添加一个类变量
private String textToDraw = null;,在paintComponent中检查该值是否非null,如果不是,绘制该字符串。在您的ActionListener中,根据您的喜好更改该字符串的值并调用repaint() -
查看编辑答案,请运行示例可编译和可运行代码。
标签: java swing user-interface graphics actionlistener