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.;
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 ( 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("u767Bu5F55");
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("u53D6u6D88");
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("u7528u6237u540D");
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("u5BC6u7801");
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("u8BB0u4F4Fu5BC6u7801");
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("u81EAu52A8u767Bu5F55");
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-u754Cu9762u8BBEu8BA1\images\background3.jpg"));
Label_background.setBounds(0, 0, 437, 272);
contentPane.add(Label_background);
}
}
② 用Java语言设计一个界面,
首先:采用什么技术实现
java语言可以使用awt 和swing等技术实现图形界面
推荐使用Swing,因为Swing比AWT更专业,更漂亮,组件更丰富,功能更强大。
2. 其次:分析采用什么布局
边界布局BorderLayout,配合表格布局GridLayout,既简单又美观
3. 最后:分析需求中需要用的组件
学生姓名 学号 显示信息 需要用到文本框JTextField
单选按钮 需要用到组件JRadioButton
复选框 需要用到组件JCheckBox
组合框 需要用到组件JComboBox
图片效果
//导入所需要的包
importjava.awt.event.*;
importjavax.swing.border.*;
importjavax.swing.*;
importjava.awt.*;
{//写一个类继承自JFrame窗体
//定义组件
=1L;
privateJPanelcontentPane;
privateJTextFieldtfName,tfNum,allInfo;
privateJRadioButtonrb1,rb2;
privateJCheckBoxcb1,cb2,cb3;
privateJComboBox<String>t1,t2,t3;
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
ClassFrameframe=newClassFrame();//创建一个窗口实例
frame.setVisible(true);//让该窗口实例可见
}catch(Exceptione){
e.printStackTrace();
}
}
});
}
/**
*窗口属性的设置,内部组件的初始化
*/
publicClassFrame(){
setTitle("选课ing...");//标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭是退出JVM
setSize(450,339);//设置窗体大小
setLocationRelativeTo(null);//窗体居中
contentPane=newJPanel();//内容面板
contentPane.setBorder(newEmptyBorder(5,5,5,5));
contentPane.setLayout(newBorderLayout(0,0));//设置布局
setContentPane(contentPane);
JPanelpanel=newJPanel(newGridLayout(5,1,5,10));//5行1列的表格布局
panel.setBorder(newTitledBorder(null,"",TitledBorder.LEADING,TitledBorder.TOP,null,null));
contentPane.add(panel,BorderLayout.CENTER);//给panel添加边框
JPanelpanel_1=newJPanel();
panel.add(panel_1);
JLabellabel=newJLabel("姓名");
panel_1.add(label);
tfName=newJTextField();
panel_1.add(tfName);
tfName.setColumns(10);
JLabellabel_2=newJLabel("学号");
panel_1.add(label_2);
tfNum=newJTextField();
tfNum.setColumns(10);
panel_1.add(tfNum);
rb1=newJRadioButton("男");
panel_1.add(rb1);
rb1.setSelected(true);//设置单选按钮中,默认选择的按钮
rb2=newJRadioButton("女");
panel_1.add(rb2);
ButtonGroupbts=newButtonGroup();//单选按钮需要加入同一个ButonGroup中才能生效
bts.add(rb1);
bts.add(rb2);
JPanelpanel_2=newJPanel();
panel.add(panel_2);
cb1=newJCheckBox("高等数学");
panel_2.add(cb1);
t1=newJComboBox<String>();
t1.setModel(newDefaultComboBoxModel<String>(newString[]{"林老师","赵老师","孙老师"}));
panel_2.add(t1);
JPanelpanel_3=newJPanel();
panel.add(panel_3);
cb2=newJCheckBox("世界经济");
panel_3.add(cb2);
t2=newJComboBox<String>();
t2.setModel(newDefaultComboBoxModel<String>(newString[]{"张老师","刘老师"}));
panel_3.add(t2);
JPanelpanel_4=newJPanel();
panel.add(panel_4);
cb3=newJCheckBox("音乐赏析");
panel_4.add(cb3);
t3=newJComboBox<String>();
t3.setModel(newDefaultComboBoxModel<String>(newString[]{"王老师","周老师"}));
panel_4.add(t3);
JPanelpanel_5=newJPanel();
panel.add(panel_5);
JButtonjbOk=newJButton("确定");
panel_5.add(jbOk);
JButtonjbRest=newJButton("重填");
panel_5.add(jbRest);
JPanelpanelSouth=newJPanel();
contentPane.add(panelSouth,BorderLayout.SOUTH);
JLabellabe=newJLabel("选课信息");
labe.setHorizontalAlignment(SwingConstants.LEFT);
panelSouth.add(labe);
allInfo=newJTextField();
allInfo.setColumns(30);
panelSouth.add(allInfo);
JPanelpanelNorth=newJPanel();
contentPane.add(panelNorth,BorderLayout.NORTH);
JLabellabelTitle=newJLabel("学生选课界面");
labelTitle.setForeground(Color.DARK_GRAY);
labelTitle.setFont(newFont("宋体",Font.BOLD,20));
panelNorth.add(labelTitle);
//给确定按钮添加事件处理代码
jbOk.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
StringBuilderinfo=newStringBuilder();
Stringname=tfName.getText();
Stringnum=tfNum.getText();
Stringsex;
if(rb1.isSelected()){
sex="男";
}else{
sex="女";
}
info.append(name+num+sex);
if(cb1.isSelected()){
Stringc=cb1.getText();
Stringt=t1.getSelectedItem().toString();
info.append(""+c+t);
}
if(cb2.isSelected()){
Stringc=cb2.getText();
Stringt=t2.getSelectedItem().toString();
info.append(""+c+t);
}
if(cb3.isSelected()){
Stringc=cb3.getText();
Stringt=t3.getSelectedItem().toString();
info.append(""+c+t);
}
allInfo.setText(info.toString());//把学生信息和选课信息放到文本框
}
});
//给重填按钮设置事件处理代码
jbRest.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
tfName.setText("");
tfNum.setText("");
rb1.setSelected(true);
cb1.setSelected(false);
t1.setSelectedIndex(0);
cb2.setSelected(false);
t2.setSelectedIndex(0);
cb3.setSelected(false);
t3.setSelectedIndex(0);
allInfo.setText("");
}
});
}
}
③ 在java中,设计图形用户界面需要经历哪几个基本步骤
1, 分析业务需求, 理顺业务逻辑
2, 根据业务需要考虑使用何种容器(JFrame ,Frame ,JWindow..) 需要的数量.
3, 每个容器(窗口)使用组件
4.组件使用何种布局方式(边界布局,绝对布局,网格布局..),排列在容器(窗口)中
5.组件中需要响应那些事件, 把事件响应代码写好,然后绑定到组件上(addListener...)
6.调试,测试
④ java怎么实现图形化界面
java图形化界面还是有很多内容要学习的,可以参考 如下实例:
publicclassTestextendsJFrame{
MyPanelmp=null;
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Testjf=newTest();
}
publicTest(){
mp=newMyPanel();
this.add(mp);
//设置标题
this.setTitle("绘图");
//设置窗体大小
this.setSize(400,300);
//设置窗体的位置
this.setLocation(100,100);
//限制窗体的大小
this.setResizable(false);
//关闭窗体时,同时退出java虚拟机
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//显示窗体
this.setVisible(true);
}
}
//定义一个MyPanel(我自己的面板,用于绘图和实现绘图区域)
classMyPanelextendsJPanel
{
//覆盖JPanel的paint方法
//Graphics是绘图的重要类,可以把它理解成一只画笔
publicvoidpaint(Graphicsg)
{
//1。调用父类函数完成初始化
super.paint(g);
////画圆
//g.drawOval(100,100,20,20);
////画直线
//g.drawLine(50,150,150,200);
////画矩形边框
//g.drawRect(150,150,30,40);
//
////设置颜色。默认为黑色
//g.setColor(Color.blue);
////填充矩形
//g.fillRect(10,10,20,30);
//画弧形
g.drawArc(200,10,100,150,120,-80);
//在面板上画图片
Imageim=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("图片路径"));
//显示图片
g.drawImage(im,10,10,200,180,this);
//画字
g.setColor(Color.red);
g.setFont(newFont("华文彩云",Font.BOLD,20));
g.drawString("要写的字",80,220);
}
}
⑤ Java编程 设计一个图形用户界面。界面包括三个单选按钮、两个复选框、一个列表、一个文本区和一个按
程序如下:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
public class JFrameDemo extends JFrame implements ActionListener
{
private JPanel panel;
private JButton button;
private JTextArea textArea;
private JCheckBox musicBox;
private JCheckBox danceBox;
private JRadioButton hanButton;
private JRadioButton manButton;
private JRadioButton huiButton;
private ButtonGroup buttonGroup;
public JFrameDemo()
{
panel = new JPanel();
button = new JButton("确定");
textArea = new JTextArea(40,30);
musicBox = new JCheckBox("唱歌"橡渗);
danceBox = new JCheckBox("跳舞梁念脊"高轮);
huiButton = new JRadioButton("回族");
hanButton = new JRadioButton("汉族");
manButton = new JRadioButton("满族");
buttonGroup = new ButtonGroup();
buttonGroup.add(huiButton);
buttonGroup.add(hanButton);
buttonGroup.add(manButton);
panel.setLayout(new FlowLayout(3));
panel.add(huiButton);
panel.add(hanButton);
panel.add(manButton);
panel.add(musicBox);
panel.add(danceBox);
panel.add(button);
panel.add(textArea);
add(panel);
setTitle("选择兴趣爱好");
setBounds(100, 100, 400, 280);
setResizable(false);
setVisible(true);
this.button.addActionListener(this);
}
public static void main(String[] args)
{
new JFrameDemo();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == this.button)
{
String info = "";
if(this.huiButton.isSelected())
{
info += this.huiButton.getText() + "\n";
}
if(this.hanButton.isSelected())
{
info += this.hanButton.getText() + "\n";
}
if(this.manButton.isSelected())
{
info += this.manButton.getText() + "\n";
}
if(this.danceBox.isSelected())
{
info += this.danceBox.getText() + "\n";
}
if(this.musicBox.isSelected())
{
info += this.musicBox.getText() + "\n";
}
this.textArea.setText(info);
}
}
}
有问题欢迎提问,满意请采纳,谢谢!