java作业:一个窗口两个按钮,按按钮分别出来不同的字。。。。。。大神帮忙啊。。。
2个回答
展开全部
代码如下:
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();
}
}
有问题欢迎提问,满意请采纳,谢谢!
追问
能不能做出来发给我啊。。。。我是个白痴了都快〒_〒
追答
已上传,需要有java环境运行,请采纳!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询