关于用JAVA创建一个GUI程序,单击窗体中的按钮时,在控制台显示“您好”。

关于用JAVA创建一个GUI程序,单击窗体中的按钮时,在控制台显示“您好”。,第1张

package baidu

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JOptionPane

public class QJButton extends JFrame{

private JButton jb

public QJButton(){

jb = new JButton("点击")

this.add(jb)

jb.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

System.out.println("您好!")

JOptionPane.showMessageDialog(QJButton.this, "您好!","消息", JOptionPane.INFORMATION_MESSAGE)

}

})

this.setSize(100,100)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

this.setVisible(true)

}

public static void main(String[] args) {

new QJButton()

}

}

顺便做了个带d出对话框的,控制台也输出了

import javax.swing.*

import javax.swing.text.*

import java.awt.*

import java.awt.event.*

/**

* @author Hardneedl

*/

final class ButtonDemo extends JFrame {

public String getTitle() {return "ButtonDemo"}

static private final Dimension size = new Dimension(600,400)

public Dimension getPreferredSize() {return size}

public Dimension getMaximumSize() {return size}

public Dimension getMinimumSize() {return size}

public Dimension getSize(){return size}

private class ButtonAction extends AbstractAction{

private JTextComponent t

private String n

private ButtonAction(String name,JTextComponent t) {

super(name)

n=name

this.t=t

}

public void actionPerformed(ActionEvent e) {

t.setText(n)

}

}

private JButton b0,b1

private JTextField t

ButtonDemo() throws HeadlessException {

init()

attachListeners()

doLay()

}

private void init(){

t=new JTextField(20)

b0=new JButton(new ButtonAction("YES",t))

b1=new JButton(new ButtonAction("NO",t))

}

private void attachListeners(){

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

}

private void doLay(){

Container container = getContentPane()

JPanel p = new JPanel()

p.add(b0)

p.add(b1)

container.add(p,BorderLayout.NORTH)

container.add(t,BorderLayout.SOUTH)

pack()

setVisible(true)

}

public static void main(String...args) {

System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel")

SwingUtilities.invokeLater(

new Runnable(){

public void run() {

new ButtonDemo()

}

}

)

}

}

dty@ubuntu:~$ cat CC.java

import java.awt.BorderLayout

import java.awt.Color

import java.awt.Container

import java.awt.FlowLayout

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

 

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JPanel

 

public class CC extends JFrame implements ActionListener {

    private JPanel jp = null

    private JButton b1 = null, b2 = null

    public CC() {

        Container c = this.getContentPane()

        //创建panel

        jp = new JPanel()

        //为panel设置底色

        jp.setBackground(Color.red)

        //jp用流水布局

        jp.setLayout(new FlowLayout())

        //创建按钮

        b1 = new JButton("红色")

        b2 = new JButton("蓝色")

        /**

         * 添加按钮事件

         */

        b1.addActionListener(this)

        b2.addActionListener(this)

        /**

         * 将按钮添加相应panel上

         */

        jp.add(b1)

        jp.add(new JLabel())

        jp.add(b2)

        /**

         * 将panel添加到容器

         */

        c.add(jp, BorderLayout.CENTER)

        this.setSize(500, 500)

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

        this.setVisible(true)

         

    }    

    public static void main(String[] args) {

        new CC()    

    }

    @Override

    public void actionPerformed(ActionEvent e) {

        // TODO Auto-generated method stub

        if (e.getSource() == b1) {

            jp.setBackground(Color.red)

        } else if (e.getSource() == b2) {

            jp.setBackground(Color.blue)

        }

    }

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11387063.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-15
下一篇2023-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存