JAVA程序:一个窗口,有两个按钮 一个标签,分别单击两个按钮,标签的内容不同
展开全部
搞定:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class HelloIFrame {
private static JTextArea area;
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setTitle("JAVA");
jf.setBounds(500, 200, 300, 300);
JPanel con = new JPanel(null);
area = new JTextArea();
area.setLineWrap(true);
JScrollPane jp = new JScrollPane(area);
jp.setBounds(10, 10, 280, 200);
con.add(jp);
JButton helloButton = new JButton("HELLO!");
JButton clearButton = new JButton("JAVA");
helloButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
area.setText("");
area.append("Hello!" + "\n");
}
});
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
area.setText("");
area.append("JAVA!" + "\n");
}
});
helloButton.setBounds(70, 220, 75, 30);
clearButton.setBounds(150, 220, 75, 30);
con.add(helloButton);
con.add(clearButton);
jf.add(con);
jf.setResizable(false);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
展开全部
楼主,依题意,程序如下:
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ShowContent extends JFrame implements ActionListener
{
private JButton buttonone;
private JButton buttontwo;
private JLabel label;
public ShowContent()
{
buttonone = new JButton("按钮1");
buttontwo = new JButton("按钮2");
label = new JLabel("等待内容");
setTitle("显示内容");
setBounds((Toolkit.getDefaultToolkit().getScreenSize().width-300)/2,
(Toolkit.getDefaultToolkit().getScreenSize().height-300)/2, 300, 200);
setLayout(new FlowLayout());
setVisible(true);
add(buttonone);
add(buttontwo);
add(label);
buttonone.addActionListener(this);
buttontwo.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == buttonone)
{
this.label.setText("我是buttonone");
}
if(e.getSource() == buttontwo)
{
this.label.setText("我是buttontwo");
}
}
public static void main(String[] args)
{
new ShowContent();
}
}
有问题欢迎提问,满意请采纳,谢谢!
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ShowContent extends JFrame implements ActionListener
{
private JButton buttonone;
private JButton buttontwo;
private JLabel label;
public ShowContent()
{
buttonone = new JButton("按钮1");
buttontwo = new JButton("按钮2");
label = new JLabel("等待内容");
setTitle("显示内容");
setBounds((Toolkit.getDefaultToolkit().getScreenSize().width-300)/2,
(Toolkit.getDefaultToolkit().getScreenSize().height-300)/2, 300, 200);
setLayout(new FlowLayout());
setVisible(true);
add(buttonone);
add(buttontwo);
add(label);
buttonone.addActionListener(this);
buttontwo.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == buttonone)
{
this.label.setText("我是buttonone");
}
if(e.getSource() == buttontwo)
{
this.label.setText("我是buttontwo");
}
}
public static void main(String[] args)
{
new ShowContent();
}
}
有问题欢迎提问,满意请采纳,谢谢!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询