当前位置:首页 > 代码 > 正文

swing代码点击按钮(swing怎么在窗体里添加按钮)

admin 发布:2022-12-19 14:46 162


本篇文章给大家谈谈swing代码点击按钮,以及swing怎么在窗体里添加按钮对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java swing 编程中,如何实现点击按钮弹出新的窗口???

其实是内部类的运用,我附上了程序源码和运行结果图片供你参考

import javax.swing.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class popupWindows{

    JFrame mainWindow;

    public popupWindows(){

        mainWindow = new JFrame("pop up window");

        //mainWindow.setBounds(10,10,10,10);

        JButton JB = new JButton("PopUp");

        JB.addActionListener(new creatNewWindow());

        mainWindow.setVisible(true);

        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mainWindow.add(JB);

        mainWindow.pack();

    }

    class creatNewWindow implements ActionListener{

        public void actionPerformed(ActionEvent e){

            new popupWindows().mainWindow.setBounds(100,100,100,100);

        }

    }

    public static void main(String[] args){

        new popupWindows();

    }

}

我想用Swing实现单击按钮后能弹出一个新窗口 也就是要做两个窗口出来!这是我的代码!谢谢!!

将你的ActionListener里边改成

public void actionPerformed(ActionEvent e)

{

JLable jl=new JLale();

JFrame frame = new JFrame("新窗口");

jl.setText("这是新窗口");

jl.setVerticalAlignment(JLable.CENTER);

jl.setHorizontaAlignment(JLable.CENTER);

frame.add(jl);

frame.setBounds(200,300,250,300);

frame.setVisible(true);

}

在java swing中如何实现通过代码按下一个按钮?

给事件队列发一个点击事件即可,按钮(JButton)本身就有一个doClick方法,或者Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent一个MouseEvent即可

我想用Swing实现单击按钮后能弹出一个新窗口!!这是我的代码!谢谢!

// 修改后的代码。里面有几个地方需要注意一下:

// 1 实现接口必须要实现接口的方法

// 2 创建新窗口必须实例化新的JFrame或JDialog对象。

// 详细的请看代码注释吧。

import javax.swing.*;

import java.awt.Rectangle;

import java.awt.event.*;

public class Swing7 extends JFrame implements ActionListener {

JButton jb = new JButton();

public Swing7() {

this.setTitle("Java——");

jb.setText("确定");

jb.setMnemonic('a');

this.add(jb);

this.setBounds(200, 300, 250, 300);

this.setVisible(true);

jb.addActionListener(this); //由于Swing7实现了ActionListener接口,所以给jb添加的ActionListener就是Swing7实例。

}

public void actionPerformed(ActionEvent e) {// 实现ActionListener接口的actionPerformed接口。

JFrame frame = new JFrame("新窗口");//构造一个新的JFrame,作为新窗口。

frame.setBounds(// 让新窗口与Swing7窗口示例错开50像素。

new Rectangle(

(int) this.getBounds().getX() + 50,

(int) this.getBounds().getY() + 50,

(int) this.getBounds().getWidth(),

(int) this.getBounds().getHeight()

)

);

JLabel jl = new JLabel();// 注意类名别写错了。

frame.getContentPane().add(jl);

jl.setText("这是新窗口");

jl.setVerticalAlignment(JLabel.CENTER);

jl.setHorizontalAlignment(JLabel.CENTER);// 注意方法名别写错了。

frame.setVisible(true);

}

public static void main(String args[]) {

Swing7 s = new Swing7();

}

}

java swing点击红色按钮背景显示红色? (框架不要改)

空中重绘即可,程序发下:

import java.awt.Button;

import java.awt.Color;

import java.awt.Container;

import java.awt.EventQueue;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

public class Background {

private JFrame frame;

private final Button redButton = new Button("红色"); //红色按钮

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

Background window = new Background();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the application.

*/

public Background() {

initialize();

}

/**

* Initialize the contents of the frame.

*/

private void initialize() {

frame = new JFrame();

frame.setBounds(100, 100, 450, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(new FlowLayout());

frame.getContentPane().add(redButton);

redButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

Container container = frame.getContentPane();

container.setBackground(Color.red);

frame.repaint();

}

});

}

}

有问题欢迎提问,满意请采纳,谢谢!

swing 点击一个按钮,根据按钮的位置(按钮位置固定的) 弹出一个 JPopupMenu ?希望高手给出代码

JPopopMenu.show(Component invoker, int x, int y);

invoker为button的父窗口,x,y为JPopopMenu的左上角坐标(以button坐标为参照)

关于swing代码点击按钮和swing怎么在窗体里添加按钮的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/9591.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载