java创建一个文本框和三个按钮的程序,当按下某个按钮时,使不同的文字显示在文本框中。我的程序编写如下
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;classPanel1extendsJPanel{...
import java.awt.*;import java.awt.event.*;import javax.swing.*;class Panel1 extends JPanel{ JTextField text; Panel1(){ text=new JTextField("请点击按钮"); add(text); }}class Panel2 extends JPanel{ JButton button1,button2,button3; Panel2(){ setLayout(new GridLayout(1,3)); button1=new JButton("red"); button2=new JButton("green"); button3=new JButton("blue"); add(button1); add(button2); add(button3); }}class ButtonClick extends JFrame implements ActionListener{ Panel1 panel1=new Panel1(); Panel2 panel2=new Panel2(); ButtonClick(){ setSize(300,400); Container conPane=getContentPane(); conPane.setLayout(new GridLayout(2,1)); conPane.add(panel1); conPane.add(panel2); pack(); } public void actionPerformed(ActionEvent e) { if (e.getSource()==panel2.button1){ panel1.text.setText("red"); } else if(e.getSource()==panel2.button2){ panel1.text.setText("green"); } else if(e.getSource()==panel2.button3){ panel1.text.setText("blue"); } }}public class Question5_7 { public static void main(String[] args) { ButtonClick bc=new ButtonClick(); bc.setVisible(true);}}当点击下面的三个按钮时,没有任何反应,请问是怎么回事?刚开始学java,请指教!
展开
3个回答
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class TestN extends JFrame{
private JTextField text;
private JButton b1,b2,b3;
private JPanel content;
public TestN(){
setSize(300,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
content = new JPanel();
content.setBorder(new EmptyBorder(5, 5, 5, 5));
content.setLayout(null);
setContentPane(content);
text = new JTextField("请点击按钮");
text.setBounds(00, 20, 200, 30);
text.setColumns(300);
content.add(text);
b1 = new JButton("red");
b1.setBounds(10, 50, 100, 20);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
text.setText("red");
}
});
content.add(b1);
b2 = new JButton("green");
b2.setBounds(110, 50, 100, 20);
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
text.setText("green");
}
});
content.add(b2);
b3 = new JButton("blue");
b3.setBounds(210, 50, 100, 20);
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
text.setText("blue");
}
});
content.add(b3);
}
public static void main(String[] args){
TestN test = new TestN();
}
}
自己参考下吧
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class TestN extends JFrame{
private JTextField text;
private JButton b1,b2,b3;
private JPanel content;
public TestN(){
setSize(300,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
content = new JPanel();
content.setBorder(new EmptyBorder(5, 5, 5, 5));
content.setLayout(null);
setContentPane(content);
text = new JTextField("请点击按钮");
text.setBounds(00, 20, 200, 30);
text.setColumns(300);
content.add(text);
b1 = new JButton("red");
b1.setBounds(10, 50, 100, 20);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
text.setText("red");
}
});
content.add(b1);
b2 = new JButton("green");
b2.setBounds(110, 50, 100, 20);
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
text.setText("green");
}
});
content.add(b2);
b3 = new JButton("blue");
b3.setBounds(210, 50, 100, 20);
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
text.setText("blue");
}
});
content.add(b3);
}
public static void main(String[] args){
TestN test = new TestN();
}
}
自己参考下吧
展开全部
ButtonClick(){ //在这个构造函数里给3个按钮加上监听
panel2.button1.addActionListener(this);
panel2.button2.addActionListener(this);
panel2.button3.addActionListener(this);
.
.
.
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
取值的地方出问题了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询