本文目录一览:
- 1、怎样用java编写图形界面的Application程序
- 2、java编写一个图形界面程序
- 3、Java编写一个图形界面
- 4、java图形界面的布局
- 5、Java编写图形用户界面程序
- 6、JAVA的图形界面是怎么出来的
怎样用java编写图形界面的Application程序
java编写图形界面需要用到swing等组件,可以在eclipse中安装windowbuilder来开发窗体,自动生成窗体代码,然后自己再根据需要修改,如:
package mainFrame;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class Mian_login extends JFrame {
private JPanel contentPane;
private JTextField text_LoginName;
private JPasswordField Login_password;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
Mian_login frame = new Mian_login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Mian_login() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 200, 443, 300);
setResizable(false);
setTitle("登 录");
/*获取系统按钮样式*/
String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(lookAndFeel);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setOpaque(false);
panel.setBounds(0, 0, 434, 272);
contentPane.add(panel);
panel.setLayout(null);
JButton btn_Login = new JButton("\u767B\u5F55");
btn_Login.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
btn_Login.setBounds(88, 195, 70, 23);
panel.add(btn_Login);
JButton btn_cancel = new JButton("\u53D6\u6D88");
btn_cancel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
dispose();
}
});
btn_cancel.setBounds(268, 195, 70, 23);
panel.add(btn_cancel);
JLabel lblNewLabel_name = new JLabel("\u7528\u6237\u540D");
lblNewLabel_name.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_name.setOpaque(true);
lblNewLabel_name.setBounds(88, 48, 70, 23);
panel.add(lblNewLabel_name);
JLabel lblNewLabel_passwd = new JLabel("\u5BC6\u7801");
lblNewLabel_passwd.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_passwd.setOpaque(true);
lblNewLabel_passwd.setBounds(88, 102, 70, 23);
panel.add(lblNewLabel_passwd);
JCheckBox chckbx_remember = new JCheckBox("\u8BB0\u4F4F\u5BC6\u7801");
chckbx_remember.setBounds(102, 150, 84, 23);
panel.add(chckbx_remember);
text_LoginName = new JTextField();
text_LoginName.setBounds(182, 48, 156, 23);
panel.add(text_LoginName);
text_LoginName.setColumns(10);
Login_password = new JPasswordField();
Login_password.setBounds(182, 102, 156, 23);
panel.add(Login_password);
JCheckBox chckbx_AutoLogin = new JCheckBox("\u81EA\u52A8\u767B\u5F55");
chckbx_AutoLogin.setBounds(233, 150, 84, 23);
panel.add(chckbx_AutoLogin);
JLabel Label_background = new JLabel("");
Label_background.setIcon(new ImageIcon("E:\\JAVA_workplace\\0002-\u754C\u9762\u8BBE\u8BA1\\images\\background3.jpg"));
Label_background.setBounds(0, 0, 437, 272);
contentPane.add(Label_background);
}
}
java编写一个图形界面程序
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.Border;
class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private MapString, Integer sizes = new HashMapString, Integer();
private MapString, Integer styles = new HashMapString, Integer();
private MapString, Integer toppings = new HashMapString, Integer();
public MainFrame() {
sizes.put("Extra Large", 10);
sizes.put("Large", 8);
sizes.put("Medium", 5);
sizes.put("Small", 3);
styles.put("Deep Dish", 20);
styles.put("Regular", 10);
styles.put("Thin Crust", 5);
styles.put("Chicago", 3);
toppings.put("Cheese", 8);
toppings.put("Tomato", 7);
toppings.put("Peppers", 6);
toppings.put("Peperoni", 5);
this.setTitle("布局及事件处理");
this.setSize(450, 350);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblTitle = new JLabel();
lblTitle.setText("Pizzeria Juno");
lblTitle.setFont(new Font("宋体", Font.BOLD, 36));
lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
this.add("North", lblTitle);
JPanel bodyPanel = new JPanel();
bodyPanel.setLayout(new GridLayout(2, 1));
this.add("Center", bodyPanel);
JPanel listPanel = new JPanel();
listPanel.setLayout(new GridLayout(1, 3));
listPanel.setSize(200, 200);
bodyPanel.add(listPanel);
Border lineBorder = BorderFactory.createLineBorder(Color.BLACK);
JPanel sizePanel = new JPanel();
sizePanel.setLayout(new BorderLayout());
listPanel.add(sizePanel);
JLabel sizeTitle = new JLabel();
sizeTitle.setText("Sizes");
sizePanel.add("North", sizeTitle);
JList sizeList = new JList(sizes.keySet().toArray());
sizeList.setSize(100, 100);
sizeList.setBorder(lineBorder);
sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sizePanel.add(sizeList);
JPanel stylePanel = new JPanel();
stylePanel.setLayout(new BorderLayout());
listPanel.add(stylePanel);
JLabel styleTitle = new JLabel();
styleTitle.setText("Styles");
stylePanel.add("North", styleTitle);
JList styleList = new JList(styles.keySet().toArray());
styleList.setSize(100, 100);
styleList.setBorder(lineBorder);
styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
stylePanel.add(styleList);
JPanel toppingPanel = new JPanel();
toppingPanel.setLayout(new BorderLayout());
listPanel.add(toppingPanel);
JLabel toppingTitle = new JLabel();
toppingTitle.setText("Toppings");
toppingPanel.add("North", toppingTitle);
JList toppingList = new JList(toppings.keySet().toArray());
toppingList.setSize(100, 100);
toppingList.setBorder(lineBorder);
toppingPanel.add(toppingList);
JTextArea txtResult = new JTextArea();
txtResult.setEditable(false);
bodyPanel.add(txtResult);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1, 3));
this.add("South", bottomPanel);
JLabel label1 = new JLabel("Click to complete order");
bottomPanel.add(label1);
JButton btnRingUp = new JButton("Ring up");
btnRingUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(sizeList.getSelectedValue() == null) {
JOptionPane.showMessageDialog(MainFrame.this, "Please select size.");
return;
}
if(styleList.getSelectedValue() == null) {
JOptionPane.showMessageDialog(MainFrame.this, "Please select style.");
return;
}
if(toppingList.getSelectedValue() == null) {
JOptionPane.showMessageDialog(MainFrame.this, "Please select topping.");
return;
}
float total = 0;
String size = sizeList.getSelectedValue().toString();
total += sizes.get(size);
String style = styleList.getSelectedValue().toString();
total += styles.get(style);
String result = size + " Pizza, " + style + " Style";
Object[] toppings = toppingList.getSelectedValues();
for(Object topping : toppings) {
result += "\n +" + topping.toString();
total += MainFrame.this.toppings.get(topping.toString());
}
result += "\n Total: " + total;
txtResult.setText(result);
}
});
bottomPanel.add(btnRingUp);
JButton btnQuit = new JButton("Quit");
btnQuit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MainFrame.this.dispose();
}
});
bottomPanel.add(btnQuit);
}
}
public class App {
public static void main(String[] args) {
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
}
}
Java编写一个图形界面
我觉得还是我的好些。。。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileOutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class TestSwing extends JFrame{
private JLabel label1, label2, label3, label4,label5,label6,label7;
private JPanel panel,panel1,panel2;
private JTextField tField1,tField2,tField3,tField4,tField5,tField6,tField7,tField8,tField9;
private JButton button;
private String filepath="c:\\info.dat";
public TestSwing(){
init();
this.setSize(300, 280);
this.setLocation(
(Toolkit.getDefaultToolkit().getScreenSize().width - this
.getSize().width) / 2,
(Toolkit.getDefaultToolkit().getScreenSize().height - this
.getSize().height) / 2);
this.setResizable(false);
this.setVisible(true);
this.setTitle("info");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init(){
initPanel();
initLabel();
initTextField();
initButton();
initContent();
}
private void initContent(){
panel1.add(label1);
panel1.add(tField1);
panel1.add(label2);
panel1.add(tField2);
panel1.add(label3);
panel1.add(tField3);
panel1.add(label4);
panel1.add(tField4);
panel1.add(label5);
panel1.add(tField5);
panel1.add(label6);
panel1.add(tField6);
panel1.add(label7);
panel1.add(tField7);
panel1.add(tField8);
panel1.add(tField9);
panel2.add(button);
panel.add(panel1,BorderLayout.CENTER);
panel.add(panel2,BorderLayout.SOUTH);
getContentPane().add(panel);
}
private void initPanel(){
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setBackground(Color.WHITE);
panel1 = new JPanel();
GridLayout upLayout = new GridLayout(8, 2);
panel1.setLayout(upLayout);
panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
}
private void initLabel(){
label1 = new JLabel("IP地址", SwingConstants.LEFT);
label2 = new JLabel("端口", SwingConstants.LEFT);
label3 = new JLabel("访问地址", SwingConstants.LEFT);
label4 = new JLabel("XXXXX", SwingConstants.LEFT);
label5 = new JLabel("XXXXX", SwingConstants.LEFT);
label6 = new JLabel("XXXXX", SwingConstants.LEFT);
label7 = new JLabel("XXXXX", SwingConstants.LEFT);
}
private void initTextField(){
tField1 = new JTextField(10);
tField2 = new JTextField(10);
tField3 = new JTextField(10);
tField4 = new JTextField(10);
tField5 = new JTextField(10);
tField6 = new JTextField(10);
tField7 = new JTextField(10);
tField8 = new JTextField(10);
tField9 = new JTextField(10);
}
private void initButton(){
button = new JButton("完成");
button.addMouseListener(new MouseAdapter(){
//左键点击
public void mouseClicked(MouseEvent e) {
String ipv = tField1.getText().trim();
String portv = tField2.getText().trim();
String addrv = tField3.getText().trim();
String xx1v = tField4.getText().trim();
String xx2v = tField5.getText().trim();
String xx3v = tField6.getText().trim();
String xx4v = tField7.getText().trim();
String inputx = tField8.getText().trim();
String inputxv = tField9.getText().trim();
createFile(ipv, portv, addrv, xx1v, xx2v, xx3v, xx4v, inputx, inputxv);
JOptionPane.showMessageDialog(null, "保存成功");
tField1.setText("");
tField2.setText("");
tField3.setText("");
tField4.setText("");
tField5.setText("");
tField6.setText("");
tField7.setText("");
tField8.setText("");
tField9.setText("");
}
});
}
public static void main(String[]args){
new TestSwing();
}
private void createFile(String ipv,String portv ,String addrv,String xx1v,String xx2v,String xx3v,String xx4v,String inputx,String inputxv){
StringBuffer context = new StringBuffer();
context.append("ip=").append(ipv).append("\n");
context.append("port=").append(portv).append("\n");
context.append("addr=").append(addrv).append("\n");
context.append("xx1=").append(xx1v).append("\n");
context.append("xx2=").append(xx2v).append("\n");
context.append("xx3=").append(xx3v).append("\n");
context.append("xx4=").append(xx4v).append("\n");
context.append(inputx).append("=").append(inputxv).append("");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filepath);
fos.write(context.toString().getBytes());
} catch (Exception e) {
} finally {
if (fos != null)
try {
fos.close();
} catch (Exception e) {
}
}
}
}
java图形界面的布局
没用过SWT的人路过,提供一个SWING:
窗口用JFrame(frame),布局为BorderLayout.
顶部为JLabel(labelTop),带图片和文字,用frame.add(labelTop, BorderLayout.NORTH)加入
左面为JPanel(panelLeft),布局为BoxLayout,参数Y_AXIS.需要在创建JPanel时设置Layout为NULL,然后再创建BoxLayout时需要把已经建立的panelLeft作为参数传进去,然后再设置panelLeft的Layout为那个BoxLayout。用frame.add(panelLeft, BorderLayout.WEST)加入
中间为一堆不同功能的JPanel,布局根据功能而定了,在点击按钮时,移除frame此时中间的panel(记录在一个自己的变量panelCenter里),然后加入按钮对应的panel,再把加入的panel存入变量panelCenter.
Java编写图形用户界面程序
运行如图
参考代码如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RegDemo extends JFrame implements ActionListener{
JTextField jtf;
JPasswordField jpf;
public RegDemo() {
//组件的创建, 和布局安排
JPanel jpc = new JPanel();//默认流式布局
JPanel jp1 = new JPanel(new GridLayout(2, 2,5,10));//网格布局
jp1.setBorder(BorderFactory.createTitledBorder("用户注册"));
JLabel jl1 = new JLabel("用户名:");
jtf = new JTextField(10);
JLabel jl2 = new JLabel("密码:");
jpf = new JPasswordField(10);
jpf.setEchoChar('*');//用*号来隐藏密码的显示
jp1.add(jl1);jp1.add(jtf);
jp1.add(jl2);jp1.add(jpf);
jpc.add(jp1);
add(jpc);
JButton jb1 = new JButton("提交");
jb1.addActionListener(this);
jb1.setActionCommand("yes");
JButton jb2 = new JButton("取消");
jb2.addActionListener(this);
jb2.setActionCommand("no");
JPanel jp2 = new JPanel();
jp2.add(jb1);jp2.add(jb2);
add(jp2,BorderLayout.SOUTH);
setTitle("用户注册界面");
setSize(280, 280);
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//
setVisible(true);
}
public static void main(String[] args) {
new RegDemo();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("yes")){
String name = jtf.getText().trim();
String pwd = new String(jpf.getPassword());
if(name.equals("")||pwd.equals("")){
JOptionPane.showMessageDialog(this, "你还没有输入用户名或者密码");
}else{
JOptionPane.showMessageDialog(this, "注册成功!用户名"+name+",密码"+pwd);
}
}else{
jtf.setText("");
jpf.setText("");
}
}
}
JAVA的图形界面是怎么出来的
,jdk 里面包含很多包,每个包都有特定的功能,最初学的是lang包是语言基础,utli是实用包提供集合日期函数随机等,而GUI 来自于awt (abstract windows tool)和 swing (slight window),这2个包提供了截面所需要的全部控件,像记事本就是典型应用QQ界面什么都可以作出来.至于applet是java 小程序,做点贪吃蛇什么的用它.至于具体的应用你可以去看java高级教程.那里还包含网络接口等.