用swing 做个增加用户界面

tangtoyin06 2009-12-29 12:38:34
想用swing做一个比较漂亮的增加用户/修改用户的界面。需要增加的属性就我自己定了。
...全文
352 23 打赏 收藏 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaining522 2009-12-31
  • 打赏
  • 举报
回复
swing漂亮到挺郁闷
你看oracle的界面。。。 难看死了!
哎 悲哀!
py330316117 2009-12-31
  • 打赏
  • 举报
回复
还真不会美化界面
huangwj20042008 2009-12-31
  • 打赏
  • 举报
回复
这只是没修饰过的界面,如果图片做的好,界面就可以很漂亮。可以把图片路径塞给上面代码里的File构造函数。
  • 打赏
  • 举报
回复
19楼的楼主试试
huangwj20042008 2009-12-31
  • 打赏
  • 举报
回复
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UserDialog extends JFrame{
private JLabel nameLbl = new JLabel("Name:");
private JLabel passwordLbl = new JLabel("Password:");
private JLabel confirmPwdLbl = new JLabel("Confirm Password:");
private JTextField nameField = new JTextField();
private JTextField passwordField = new JTextField();
private JTextField confirmPwdField = new JTextField();
private JButton confirmBtn = new JButton("Confirm");
private JButton cancelBtn = new JButton("Cancel");


public UserDialog(String title){
super(title);
initComponents();
}
public void initComponents(){
Image backgroundImage = null;
try{
backgroundImage = ImageIO.read(new File(""));
}catch(Exception ex){

}
JPanel mainPanel = new BackgroundPanel(backgroundImage);

Color backgroundColor = this.getBackground();


mainPanel.setBackground(backgroundColor);
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
GridBagLayout gridBagLayout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
JPanel centerPanel = new JPanel();
centerPanel.setBackground(backgroundColor);
centerPanel.setLayout(gridBagLayout);
centerPanel.setBorder(BorderFactory.createEmptyBorder(5,10,5,5));
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,10,5,5);
gridBagLayout.setConstraints(nameLbl, constraints);
centerPanel.add(nameLbl);
constraints.weightx = 1.0;
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagLayout.setConstraints(nameField, constraints);
centerPanel.add(nameField);
constraints.gridwidth = GridBagConstraints.RELATIVE;
constraints.weightx = 0.0;
gridBagLayout.setConstraints(passwordLbl, constraints);
centerPanel.add(passwordLbl);
constraints.weightx = 1.0;
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagLayout.setConstraints(passwordField, constraints);
centerPanel.add(passwordField);
constraints.gridwidth = GridBagConstraints.RELATIVE;
constraints.weightx = 0.0;
gridBagLayout.setConstraints(confirmPwdLbl, constraints);
centerPanel.add(confirmPwdLbl);
constraints.weightx = 1.0;
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagLayout.setConstraints(confirmPwdField, constraints);
centerPanel.add(confirmPwdField);
mainPanel.add(centerPanel,BorderLayout.CENTER);
JPanel southPanel = new JPanel();
southPanel.setBackground(backgroundColor);
southPanel.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
southPanel.setBorder(BorderFactory.createEmptyBorder(5,10,20,10));
southPanel.add(confirmBtn);
southPanel.add(Box.createHorizontalStrut(30));
southPanel.add(cancelBtn);
mainPanel.add(southPanel,BorderLayout.SOUTH);
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
c.add(mainPanel,BorderLayout.CENTER);

}
class BackgroundPanel extends JPanel{
private Image backgroundImage;
public BackgroundPanel(Image image){
this.backgroundImage = image;
}
public void paintComponent(Graphics g){
int w = this.getWidth();
int h = this.getHeight();
g.setColor(this.getBackground());
g.fillRect(0, 0, w, h);
if(backgroundImage != null){
g.drawImage(backgroundImage, 0, 0, w, h, null);
}
}
}
public static void main(String[] args){
JFrame userDialog = new UserDialog("User Dialog");
userDialog.setBounds(200, 100, 300, 400);
userDialog.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
userDialog.setVisible(true);
}
}
bobo364 2009-12-31
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 nihaozhangchao 的回复:]
O'Reilly:Java Swing(第二版)Java Swing,Second Edition 
[/Quote]

SambaGao 2009-12-31
  • 打赏
  • 举报
回复
O'Reilly:Java Swing(第二版)Java Swing,Second Edition
javahuazai 2009-12-31
  • 打赏
  • 举报
回复
做做看!
zhuyouyong 2009-12-31
  • 打赏
  • 举报
回复
怎么才叫漂亮
dajiadebeibei9 2009-12-29
  • 打赏
  • 举报
回复
setLookAndFeel
tangtoyin06 2009-12-29
  • 打赏
  • 举报
回复
只要不是很乱就行...
JamnyXie 2009-12-29
  • 打赏
  • 举报
回复
觉得java自身不擅长于编写界面。在没有美工支持下,更难做出漂亮的界面。
布局做出来美观整洁就不错了。

[Quote=引用 1 楼 healer_kx 的回复:]
怎么算是漂亮啊?
[/Quote]
healer_kx 2009-12-29
  • 打赏
  • 举报
回复
怎么算是漂亮啊?
hwlhwj 2009-12-29
  • 打赏
  • 举报
回复
楼上所说的是最快速的办法了。
teemai 2009-12-29
  • 打赏
  • 举报
回复
myeclipse中有swing的图形编辑窗口,编辑好直接有代码。。
jciv002 2009-12-29
  • 打赏
  • 举报
回复
整洁 点好
realreachard 2009-12-29
  • 打赏
  • 举报
回复
想好看,用用swing的ui框架,例如swingX等等
whereusejava 2009-12-29
  • 打赏
  • 举报
回复
别想啊,动手做吧
luffyke 2009-12-29
  • 打赏
  • 举报
回复
SWING很难做漂亮的!用setLookAndFeel方法是可行的!
用SWT/JFACE吧!!!!!!!
njj0125 2009-12-29
  • 打赏
  • 举报
回复
dingdingdingdingdingdingdingdingding
加载更多回复(3)

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧