
dty@ubuntu:~$ cat CC.java
import java.awt.BorderLayoutimport 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)
}
}
}
楼上的 搜来的代码 也要改改才能符合楼主的要求吧…… 我两个都写到一起了 import java.awt.*import javax.swing.*import java.awt.BorderLayout
import java.awt.Rectangle
import java.awt.event.MouseEvent
import java.awt.event.MouseAdapter
import java.awt.event.ActionListener
import java.awt.event.ActionEvent
import javax.swing.event.DocumentListener
import javax.swing.event.DocumentEventpublic class FrameTest extends JFrame implements DocumentListener{
BorderLayout borderLayout1 = new BorderLayout()
JPanel jPanel1 = new JPanel()
JPasswordField jPasswordField1 = new JPasswordField()
JTextField jTextField1 = new JTextField()
JMenuBar jMenuBar1 = new JMenuBar()
JMenu jMenu1 = new JMenu()
JMenu jMenu2 = new JMenu()
JMenu jMenu3 = new JMenu()
JMenuItem jMenuItem1 = new JMenuItem()
JMenuItem jMenuItem2 = new JMenuItem()
JLabel jLabel1 = new JLabel()
JLabel jLabel2 = new JLabel() public FrameTest() {
try {
jbInit()
} catch (Exception exception) {
exception.printStackTrace()
}
}private void jbInit() throws Exception {
jMenu1.setText("文件")
jMenu2.setText("编辑")
jMenu2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null, "菜单“编辑”被点击")
}
}) jMenu3.setText("帮助")
jMenu3.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null, "菜单“帮助”被点击")
}
}) jMenuItem1.setText("打开")
jMenuItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "菜单项“打开”被点击")
}
})
jMenuItem2.setText("退出")
jMenuItem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "菜单项“退出”被点击,系统关闭")
System.exit(0)
}
})
jLabel1.setText("密码:")
jLabel1.setBounds(new Rectangle(43, 75, 42, 15))
jLabel2.setText("显示密码:")
jLabel2.setBounds(new Rectangle(35, 128, 82, 15)) jPanel1.add(jPasswordField1)
jPanel1.add(jTextField1)
jPanel1.add(jLabel1)
jPanel1.add(jLabel2)
jMenuBar1.add(jMenu1)
jMenuBar1.add(jMenu2)
jMenuBar1.add(jMenu3)
jMenu1.add(jMenuItem1)
jMenu1.add(jMenuItem2)
jTextField1.setText("")
jTextField1.setBounds(new Rectangle(156, 123, 135, 25))
jPasswordField1.setText("")
jPasswordField1.getDocument().addDocumentListener(this)
jPasswordField1.setBounds(new Rectangle(156, 70, 135, 25))
jPanel1.setLayout(null) this.setJMenuBar(jMenuBar1)
this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER)
this.setSize(500, 300)
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize()
Dimension frameSize = this.getSize()
if (frameSize.height >screenSize.height) {
frameSize.height = screenSize.height
}
if (frameSize.width >screenSize.width) {
frameSize.width = screenSize.width
}
this.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2) this.setResizable(false)
this.setVisible(true)
this.setDefaultCloseOperation(EXIT_ON_CLOSE)
}public void changedUpdate(DocumentEvent e) {
jTextField1.setText(String.valueOf(jPasswordField1.getPassword()))
}public void insertUpdate(DocumentEvent e) {
jTextField1.setText(String.valueOf(jPasswordField1.getPassword()))
}public void removeUpdate(DocumentEvent e) {
jTextField1.setText(String.valueOf(jPasswordField1.getPassword()))
}
public static void main(String[] args) {
new FrameTest()
}
}
步骤:1、 引包
import java.awt.*import javax.swing.*
2、 继承JFrame
public class Demo3 extends JFrame {}
3、 定义需要的组件
//3.定义组件
JButton jb1,jb2,jb3,jb4,jb5int size=9
JButton jbs[]=new JButton[size] //先分配初始空间 4、 创建组件
//4.创建组件 jb1=new JButton("中部")//创建组件
for(int i=0i<sizei++) {
jbs[i]=new JButton(String.valueOf(i))
}
5、 设置布局管理器
//设置布局管理器,默认是的边界布局管理器 this.setLayout(new FlowLayout(FlowLayout.LEFT))//流式布局 this.add(jb1,BorderLayout.CENTER) //边界布局 this.setLayout(new GridLayout(3,3,10,10)) //网格布局
this.setLayout(null) //取消布局管理器
6、 添加组件
//添加组件 this.add(jb1)
this.add(jb2)
7、 设置窗体属性
//设置窗体属性 this.setTitle("流式布局案例") //设置窗体标题 this.setSize(300, 200) //设置窗体大小 this.setLocation(200, 400) //设置初始位置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) //关闭窗体时关闭虚拟机
//禁止用户改变窗体大小
this.setResizable(false) //7.显示窗体
this.setVisible(true)
8、 显示窗体
利用数组创建组件:
//定义组件 int size=9
JButton jbs[]=new JButton[size] //数组要先分配初始空间 //创建组件
for(int i=0i<sizei++) { jbs[i]=new JButton(String.valueOf(i))
}
//添加组件 for(int i=0i<sizei++) { this.add(jbs[i])
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)