java怎么能先在显示器输入再d出用户界面

java怎么能先在显示器输入再d出用户界面,第1张

1、在父窗口中添加一个颂中按钮

2、单击此按钮d出搭桐一个子知樱坦窗口。

3、建立父窗口类MainWindow.java。

4、建立子窗口类SubWindow.java。

5、建立主类TestGUI_Demo1.java。

6、运行测试结果,完成后即可。

import java.awt.*

public Test extends Applet

{

public init()

{

Panel p1=new Panel()/漏悄/这个就是定义啦,定义一个panel

Button bt=new Button("button")//这个就余运是定义按钮

//Button bt2=new Button("button")

p1.add(bt)/竖搜梁/到panel里添加按钮

//p1.add(bt2)

this.add(p1)//吧panel添加到窗口中

show()//这个方法还用吧。。。

}

}

import java.awt.Color

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JPanel

import javax.swing.SwingUtilities

public class TestWin extends JFrame implements ActionListener {

private JButton blackBtn=new JButton("Black")

private JButton whiteBtn=new JButton("White")

private JPanel pane=new JPanel()

 public TestWin() {

  this.blackBtn.addActionListener(this)

  this.whiteBtn.addActionListener(this)

  JPanel btnPane=new JPanel()

  btnPane.add(this.blackBtn)

  btnPane.add(this.whiteBtn)

  this.add(btnPane,"North")

  this.add(pane,"Center")

  

  this.setSize(800, 600)

  this.setLocationRelativeTo(null)

  this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

  this.setVisible(true)

 }

 @Override

 public void actionPerformed(ActionEvent e) {

  Object source=e.getSource()

  if(source==this.blackBtn) {

   this.pane.setBackground(Color.BLACK)

  }else if(source==this.whiteBtn) {

   this.pane.setBackground(Color.WHITE)

  }

 }

 public static void main(String[] args) {

  SwingUtilities.invokeLater(() -> new TestWin())

 }

}


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

原文地址: https://www.outofmemory.cn/bake/11993667.html

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

发表评论

登录后才能评论

评论列表(0条)

保存