将jbuttons添加到GUI应用程序 [英] Add jbuttons to a GUI application

查看:96
本文介绍了将jbuttons添加到GUI应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是我的按钮根本没有出现。



我尝试过的事情:



包journal.pkg7a;

/ **

*

* @author stephenwessels

* /

导入javax.swing。*;

import java.awt。*;

import java.awt.event。*;

公共类Journal7A扩展JFrame实现ActionListener

{

容器内容=新容器();

JButton btnGreen =新JButton(绿色);

JButton btnBlue =新JButton(蓝色);

JButton btnRed =新JButton( 红色);



public Journal7A()

{

content.setLayout(new FlowLayout() );

content.add(btnBlue,BorderLayout.SOUTH);

content.add(btnGreen,BorderLayout.SOUTH);

内容。 add(btnRed,BorderLayout.SOUTH);



th is.setVisible(true);

this.setSize(300,300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setTitle (第一个GUI应用程序);





btnBlue.addActionListener(this);

btnGreen.addActionListener (这);

btnRed.addActionListener(this);

}

@Override

public void actionPerformed( ActionEvent e)

{

JButton btn =(JButton)e.getSource();

if(btn == btnBlue)

content.setBackground(Color.BLUE);

else

content.setBackground(Color.GREEN);

内容。 setBackground(Color.RED);

}



/ **

* @param args命令行参数

* /

public static void main(String [] args)

{

Journal7A gui = new Journal7A();

}



}

I'm having an issue where my buttons don't show up at all.

What I have tried:

package journal.pkg7a;
/**
*
* @author stephenwessels
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Journal7A extends JFrame implements ActionListener
{
Container content = new Container();
JButton btnGreen = new JButton("Green");
JButton btnBlue = new JButton("Blue");
JButton btnRed = new JButton("Red");

public Journal7A()
{
content.setLayout(new FlowLayout());
content.add(btnBlue, BorderLayout.SOUTH);
content.add(btnGreen, BorderLayout.SOUTH);
content.add(btnRed, BorderLayout.SOUTH);

this.setVisible(true);
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("First GUI App");


btnBlue.addActionListener(this);
btnGreen.addActionListener(this);
btnRed.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton) e.getSource();
if(btn == btnBlue)
content.setBackground(Color.BLUE);
else
content.setBackground(Color.GREEN);
content.setBackground(Color.RED);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Journal7A gui = new Journal7A();
}

}

推荐答案

您的 Journal7A 类不需要继承 JFrame 。相反,您可以将您的类扩展到 JPanel ,并在您的主中创建框架对象。

There is no need for your Journal7A class to subclass JFrame. Instead, you can extends your class to JPanel, and create frame object in your main.
public class Journal7A extends JPanel implements ActionListener {

    JPanel content = new JPanel();
    JButton btnGreen = new JButton("Green");
    JButton btnBlue = new JButton("Blue");
    JButton btnRed = new JButton("Red");

    public Journal7A() {
        content.setLayout(new FlowLayout());
        content.add(btnBlue, BorderLayout.SOUTH);
        content.add(btnGreen, BorderLayout.SOUTH);
        content.add(btnRed, BorderLayout.SOUTH);
        btnBlue.addActionListener(this);
        btnGreen.addActionListener(this);
        btnRed.addActionListener(this);
        add(content);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton btn = (JButton) e.getSource();
        if (btn == btnBlue) {
            content.setBackground(Color.BLUE);
        } else {
            content.setBackground(Color.GREEN);
        }
        content.setBackground(Color.RED);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Journal7A gui = new Journal7A();
        Frame frame = new JFrame();
        frame.add(gui);
        frame.pack();
        frame.setSize(300, 300);
        frame.setTitle("First GUI App");
        frame.setVisible(true);
    }

}



如果您想使用自己的代码,请添加


If you like to use your own code , add

add(content);

btnRed.addActionListener(this);


这篇关于将jbuttons添加到GUI应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆