java swing示例
Posted 乐之者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java swing示例相关的知识,希望对你有一定的参考价值。
该范例主要是JFrame(框架)和Jpanel(画板),在Jpanel容器上添加控件,然后再把Jpanel放进JFrame的容器里面。
FrameDemo.java
import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; public class FrameDemo extends JFrame{ public static void main(String[] args) throws Exception { //获取主显示器屏幕大小 Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int screenWidth = screenSize.width; int screenHeight = screenSize.height; //设置界面组件 ComponentDemo component = new ComponentDemo(); FrameDemo frameDemo = new FrameDemo(); frameDemo.setContentPane(component); //将Panel放入到Frame中 frameDemo.setSize(400, 500); frameDemo.setLocation(screenWidth / 2 - 390 / 2, screenHeight / 2 - 580 / 2); frameDemo.setVisible(true); } }
ComponentDemo.java
package com.example.swingdemo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
public class ComponentDemo extends JPanel implements ActionListener {
//JLabel是标签,JTextField是文本框
private JLabel phoneLabel = null;
private JTextField phoneText = null;
private JButton btnQuery = null;
private JLabel contentLabel = null;
private JTextPane contentPane = null;
public ComponentDemo() {
super();
initialize();
}
private void initialize() {
phoneLabel = new JLabel();
phoneLabel.setBounds(new java.awt.Rectangle(10, 10, 70, 22));
phoneLabel.setText("电话号码:");
this.setLayout(null);
this.add(phoneLabel, null);
this.add(getPhoneText(), null);
this.add(getBtnQuery(), null);
this.add(getContentLabel(), null);
this.add(getContentPaneText(), null);
}
private JTextField getPhoneText() {
if (phoneText == null) {
phoneText = new JTextField();
phoneText.setBounds(new java.awt.Rectangle(80, 10, 100, 22));
}
return phoneText;
}
private JLabel getContentLabel() {
if (contentLabel == null) {
contentLabel = new JLabel();
contentLabel.setText("内容如下:");
contentLabel.setBounds(new java.awt.Rectangle(10, 70, 70, 22));
}
return contentLabel;
}
private JTextPane getContentPaneText() {
if (contentPane == null) {
contentPane = new JTextPane();
contentPane.setEditable(false);
contentPane.setBounds(new java.awt.Rectangle(10, 90, 350, 150));
}
return contentPane;
}
private JButton getBtnQuery() {
if (btnQuery == null) {
btnQuery = new JButton("查询UserId");
btnQuery.setBounds(new java.awt.Rectangle(10, 45, 100, 22));
// btnQuery.setText("查询");
btnQuery.addActionListener(this); //注册监听事件
}
return btnQuery;
}
// private OperateAction business = null;
// static Logger logger = Logger.getLogger(UserIdComponent.class.getName());
public void actionPerformed(ActionEvent e) {
//在这里添加按下按钮后的触发事件
try {
String btnName = e.getActionCommand();
if (btnName.equals("查询UserId")) {
contentPane.setEditable(true); //设置文本框为可编辑
String phoneNumber=phoneText.getText(); //获取电话号码
String outputString="phoneNumber是"+phoneNumber+"\\nUserId我并不知道啦\\n"+"啦啦啦/啦啦啦/我是可爱的小胖妞^_^";
contentPane.setText(outputString);
}
} catch (Exception e1) {
StringWriter trace = new StringWriter();
e1.printStackTrace(new PrintWriter(trace));
}
}
}
代码运行结果如下:
以上是关于java swing示例的主要内容,如果未能解决你的问题,请参考以下文章