java 按键框架,如何在java中更改框架?
Let's say there is a button and if you will click that, a new frame will appear and so on...
解决方案
The setVisible(true); function is used to display a frame. Create an object of the desired frame and call this function. Something like this
//The applications first or the main frame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainFrame extends JFrame {
private JButton myFirstButton;
private JButton mySecondButton;
// Constructor for a new frame
public MainFrame {
super("My First Button Program");
myFirstButton = new JButton("First Frame");
myFirstButton.setFont(new Font( "Arial", Font.BOLD, 18));
myFirstButton.setBackground(Color.red);
mySecondButton = new JButton("New Frame");
mySecondButton.setFont(new Font( "Arial", Font.BOLD, 18));
mySecondButton.setBackground(Color.green);
Container c = getContentPane();
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
c.setLayout(fl);
c.add (myFirstButton);
c.add (mySecondButton);
ButtonHandler handler = new ButtonHandler(); //creation of a new Object
myFirstButton.addActionListener(handler); // Attach/register handler to myFirstButton
mySecondButton.addActionListener(handler); //Attach/register handler to mySecondButton
setSize(400, 300);
show();
}
public static void main(String [] args) {
// Make frame
MainFrame f = new MainFrame();
f.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// This closes the window and terminates the
// Java Virtual Machine in the event that the
// Frame is closed by clicking on X.
System.out.println("Exit via windowClosing.");
System.exit(0);
}
}
);
} // end of main
// inner class for button event handling
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == myFirstButton) {
new NewFrame1();
}
if (e.getSource() == mySecondButton) {
new NewFrame2();
}
}
} // end of inner class
} // end of outer class
The frame to be opened for first button
//import statements here
public class NewFrame1 extends JFrame implements ActionListener
{
//initialises the frame and opens it
public NewFrame1()
{
JButton open = new JButton("New Window");
open.addActionListener(this);
add(open);
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
//code for the new frame
}
}
The frame to be opened for second button
//import statements here
public class NewFrame2 extends JFrame implements ActionListener
{
//initialises the frame and opens it
public NewFrame2()
{
JButton open = new JButton("New Window");
open.addActionListener(this);
add(open);
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
//code for the new frame
}
}
java 按键框架,如何在java中更改框架?相关推荐
- java jtextarea.setfont_如何在JTextArea中更改文本颜色?
既然是可能的用途Highlighter(或HTML)用于JTextArea,此api实现了样式文本的简化选项.import java.awt.*; import javax.swing.*; impo ...
- 好系统教你如何在Windows中更改文件夹的图标
如何在Windows中更改文件夹的图标 浏览到要更改其图标的文件夹的位置,或创建一个新文件夹. 右键单击该文件夹,然后从出现的下拉菜单底部选择"属性". 在文件夹属性菜单中,选择自 ...
- linux 更改ssh端口_如何在Linux中更改SSH端口-简易指南
linux 更改ssh端口 The default port on SSH is 22. But for security reasons, it's a good idea to change SS ...
- linux中更改用户密码_如何在Linux中更改用户密码
linux中更改用户密码 In this tutorial, we will focus on how you can change a user's password in Linux. We wi ...
- linux重启网卡命令_如何在 Linux 中更改 MAC 地址 | Linux 中国
在向你展示如何在 Linux 中更改 Mac 地址之前,让我们首先讨论为什么要更改它.-- Dimitrios Savvopoulos 在向你展示如何在 Linux 中更改 MAC 地址之前,让我们首 ...
- python绘图背景透明_如何在 Matplotlib 中更改绘图背景
介绍Matplotlib是Python中使用最广泛的数据可视化库之一.无论是简单还是复杂的可视化项目,它都是大多数人的首选库.在本教程中,我们将研究如何在Matplotlib中更改绘图的背景.导入数据 ...
- outlook附加文件超过服务器允许大小,如何在Outlook中更改或增加附件大小限制?...
如何在Outlook中更改附件大小限制? 有时,我无法在Outlook中成功插入大附件,但是出现警告对话框,提示附件大小超出了允许的限制. 请参阅以下屏幕截图. 发生此错误是由于超出了Outlook中 ...
- Microsoft Word 教程:如何在 Word 中更改行距?
欢迎观看 Microsoft Word 教程,小编带大家学习 Microsoft Word 的使用技巧,了解如何在 Word 中更改行距. 在 Word 中更改文档中的行距,节省文档的空间或按所需效果 ...
- python画图修改背景颜色_如何在 Matplotlib 中更改绘图背景的实现
介绍 Matplotlib是Python中使用最广泛的数据可视化库之一.无论是简单还是复杂的可视化项目,它都是大多数人的首选库. 在本教程中,我们将研究如何在Matplotlib中更改绘图的背景. 导 ...
- Microsoft Word 教程:如何在 Word 中更改页面方向、为页面添加边框?
欢迎观看 Microsoft Word 教程,小编带大家学习 Microsoft Word 的使用技巧,了解如何在 Word 中更改页面方向.为页面添加边框. 将页面方向更改为横向或纵向,在 word ...
最新文章
- IDEA 连接MySQL数据库
- hdu3182 状态压缩dp
- 二进制数代替数组做标记
- 每天学一点flash(40) 制作走马灯四
- GCC,Clang,make,cmake,makefile,CMakeLists.txt的概念
- php reverseShell
- 终于有人把Knative讲明白了
- CTA策略02_boll
- vb.net VS2010编程
- addEventListener和attachEvent的区别(转载)
- python爬取豆瓣电影top250并保存为xlsx_批量抓取豆瓣电影TOP250数据
- b和kb的换算_KB和MB怎么换算?
- 4400php兑换美金是多少,美金换算人民币怎么算(美金换人民币怎样计算)
- SQL Server select 英文区分大小写
- 迁移学习VGG16实现猫狗大战
- Unity 5.3制作VR项目
- 视频播放过程中做视频问答(视频弹题功能)
- 蚂蚁开源 - 聚沙成塔,相信社区的力量 | ChinaOSC
- 『这辈子就相爱《何苦要等下辈子》 李草青青、肖玄MV』
- MySQL——初识MySQL
热门文章
- 7.PHP Cookie与Session
- hdu2167 方格取数 状态压缩dp
- 操作系统原理第七章:死锁
- 【错误记录】Android Studio 编译报错 ( SDK location not found )
- 【Android NDK 开发】JNI 方法解析 ( C/C++ 调用 Java 方法 | 函数签名 | 调用对象方法 | 调用静态方法 )
- 【网站部署】解析二级域名并部署网站
- ELK之收集日志到mysql数据库
- bool 字符串方法 和for循环
- 反射(操作MetaData)
- python 3.0内置函数map、filter