用Java编写程序实现:按下第一个按钮在文本框中显示:“我按下了按钮”,按下第二个
编写程序实现:按下第一个按钮在文本框中显示:“我按下了按钮”,按下第二个按钮将背景颜色换成蓝色,点击右上角关闭按钮实现窗口的关闭...
编写程序实现:按下第一个按钮在文本框中显示:“我按下了按钮”,按下第二个按钮将背景颜色换成蓝色,点击右上角关闭按钮实现窗口的关闭
展开
4个回答
展开全部
public class Test extends JFrame{
private JTextField jtf_text;
JPanel panel;
public Test() {
this.setBounds(0, 0, 320, 480);
panel = new JPanel();
panel.setLayout(null);
this.getContentPane().add(panel);
{
JButton jbtn_down = new JButton("\u6211\u6309\u4E0B\u4E86\u6309\u94AE");
jbtn_down.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
setText();
}
});
jbtn_down.setBounds(27, 151, 126, 23);
panel.add(jbtn_down);
}
{
JButton jbtn_color = new JButton("\u53D8\u84DD");
jbtn_color.setBounds(185, 151, 93, 23);
jbtn_color.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
setBackgroundColor();
}
});
panel.add(jbtn_color);
}
{
jtf_text = new JTextField();
jtf_text.setBounds(58, 60, 188, 20);
panel.add(jtf_text);
jtf_text.setColumns(10);
}
this.addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
//退出
public void windowClosing(WindowEvent e) {
System.exit(1);
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
});
}
//设置文字
public void setText(){
jtf_text.setText("我按下了按钮");
}
//设置颜色
public void setBackgroundColor(){
panel.setBackground(Color.BLUE);
}
public static void main(String[] args) {
new Test().setVisible(true);
}
}
private JTextField jtf_text;
JPanel panel;
public Test() {
this.setBounds(0, 0, 320, 480);
panel = new JPanel();
panel.setLayout(null);
this.getContentPane().add(panel);
{
JButton jbtn_down = new JButton("\u6211\u6309\u4E0B\u4E86\u6309\u94AE");
jbtn_down.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
setText();
}
});
jbtn_down.setBounds(27, 151, 126, 23);
panel.add(jbtn_down);
}
{
JButton jbtn_color = new JButton("\u53D8\u84DD");
jbtn_color.setBounds(185, 151, 93, 23);
jbtn_color.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
setBackgroundColor();
}
});
panel.add(jbtn_color);
}
{
jtf_text = new JTextField();
jtf_text.setBounds(58, 60, 188, 20);
panel.add(jtf_text);
jtf_text.setColumns(10);
}
this.addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
//退出
public void windowClosing(WindowEvent e) {
System.exit(1);
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
});
}
//设置文字
public void setText(){
jtf_text.setText("我按下了按钮");
}
//设置颜色
public void setBackgroundColor(){
panel.setBackground(Color.BLUE);
}
public static void main(String[] args) {
new Test().setVisible(true);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ButtonTest extends JFrame{
private static final long serialVersionUID = -4204413680134187982L;
private JPanel panel = new JPanel(null);
private JButton button1 = new JButton("出字儿");
private JButton button2 = new JButton("变色儿");
private JTextArea textArea = new JTextArea();
{
setSize(600, 400);
setContentPane(panel);
panel.add(button1);button1.setBounds(100, 300, 100, 30);
panel.add(button2);button2.setBounds(400, 300, 100, 30);
panel.add(textArea);textArea.setBounds(100, 50, 400, 200);
}
{
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText("我按下了按钮\n");
panel.setBackground(Color.WHITE);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setBackground(Color.BLUE);
textArea.setText("我是文本域,不是底板,所以不变色!");
}
});
}
public ButtonTest() {
setLocation(100, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new ButtonTest();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ButtonTest extends JFrame{
private static final long serialVersionUID = -4204413680134187982L;
private JPanel panel = new JPanel(null);
private JButton button1 = new JButton("出字儿");
private JButton button2 = new JButton("变色儿");
private JTextArea textArea = new JTextArea();
{
setSize(600, 400);
setContentPane(panel);
panel.add(button1);button1.setBounds(100, 300, 100, 30);
panel.add(button2);button2.setBounds(400, 300, 100, 30);
panel.add(textArea);textArea.setBounds(100, 50, 400, 200);
}
{
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText("我按下了按钮\n");
panel.setBackground(Color.WHITE);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setBackground(Color.BLUE);
textArea.setText("我是文本域,不是底板,所以不变色!");
}
});
}
public ButtonTest() {
setLocation(100, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new ButtonTest();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestFrame extends Frame implements ActionListener{
Panel p,p1,p2,p3,p4,p5,p6,p_login;
Button bLogin , bExit;
Label lUserName;
TextField tUserName;
public TestFrame(String s){
super(s);
mainFrame();
}
public void mainFrame(){
this.setLayout(new BorderLayout());
p=new Panel();
p_login=new Panel();
p1=new Panel();
p2=new Panel();
p5=new Panel();
p6=new Panel();
p_login.setLayout(new GridLayout(3,2));
bLogin=new Button("第一个按钮");
bLogin.addActionListener(this);
bExit =new Button("第二个按钮");
bExit.addActionListener(this);
lUserName=new Label("显示");
tUserName=new TextField(20);
tUserName.addActionListener(this);
p1.add(lUserName);
p2.add(tUserName);
p5.add(bLogin);
p6.add(bExit);
p_login.add(p1);
p_login.add(p2);
p_login.add(p5);
p_login.add(p6);
p.add(p_login);
this.add(p,"Center");
this.setBounds(200, 200, 600, 400);
this.addWindowListener (new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible (false);
System.exit(0);
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new TestFrame("显示");
}
public void actionPerformed(ActionEvent e) {
Button btn=(Button) e.getSource();
if(btn==bExit){
p.setBackground(Color.BLUE);
p1.setBackground(Color.BLUE);
p2.setBackground(Color.BLUE);
p5.setBackground(Color.BLUE);
p6.setBackground(Color.BLUE);
p_login.setBackground(Color.BLUE);
lUserName.setBackground(Color.BLUE);
tUserName.setBackground(Color.BLUE);
bExit.setBackground(Color.BLUE);
bLogin.setBackground(Color.BLUE);
}
if(btn==bLogin){
tUserName.setText("我按下了第一个按钮");
}
}
}
不知道 是不是 这个意思 希望采纳 嘿嘿
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestFrame extends Frame implements ActionListener{
Panel p,p1,p2,p3,p4,p5,p6,p_login;
Button bLogin , bExit;
Label lUserName;
TextField tUserName;
public TestFrame(String s){
super(s);
mainFrame();
}
public void mainFrame(){
this.setLayout(new BorderLayout());
p=new Panel();
p_login=new Panel();
p1=new Panel();
p2=new Panel();
p5=new Panel();
p6=new Panel();
p_login.setLayout(new GridLayout(3,2));
bLogin=new Button("第一个按钮");
bLogin.addActionListener(this);
bExit =new Button("第二个按钮");
bExit.addActionListener(this);
lUserName=new Label("显示");
tUserName=new TextField(20);
tUserName.addActionListener(this);
p1.add(lUserName);
p2.add(tUserName);
p5.add(bLogin);
p6.add(bExit);
p_login.add(p1);
p_login.add(p2);
p_login.add(p5);
p_login.add(p6);
p.add(p_login);
this.add(p,"Center");
this.setBounds(200, 200, 600, 400);
this.addWindowListener (new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible (false);
System.exit(0);
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new TestFrame("显示");
}
public void actionPerformed(ActionEvent e) {
Button btn=(Button) e.getSource();
if(btn==bExit){
p.setBackground(Color.BLUE);
p1.setBackground(Color.BLUE);
p2.setBackground(Color.BLUE);
p5.setBackground(Color.BLUE);
p6.setBackground(Color.BLUE);
p_login.setBackground(Color.BLUE);
lUserName.setBackground(Color.BLUE);
tUserName.setBackground(Color.BLUE);
bExit.setBackground(Color.BLUE);
bLogin.setBackground(Color.BLUE);
}
if(btn==bLogin){
tUserName.setText("我按下了第一个按钮");
}
}
}
不知道 是不是 这个意思 希望采纳 嘿嘿
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是jsp吗 还是纯java代码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询