java Swing UI编写图形界面


利用Swing UI编写图形界面


实例代码:

package rzjm;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
public class 界面3 extends JFrame {
private JPanel contentPane;
private JFrame frame=new JFrame("图形界面");//基于移动机器人的语音控制系统
    private JPanel imagePanel;
private ImageIcon background;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
界面3 frame = new 界面3();
frame.setVisible(false);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public 界面3() {
//背景图片#
background = new ImageIcon("C:\\Users\\lenovo\\Desktop\\5445c436621e19c3128b4eb7-1-hd_mh1517213885532.jpg");
        //把背景图片显示在一个标签里面#
JLabel label = new JLabel(background);
//把标签的大小位置设置为图片刚好填充整个面板#
        label.setBounds(0, 0, background.getIconWidth(),
background.getIconHeight());
//把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明#
        imagePanel = (JPanel) frame.getContentPane();
imagePanel.setOpaque(false);
        //内容窗格默认的布局管理器为BorderLayout#
imagePanel.setLayout(new BorderLayout(5,5));
JButton b1 = new JButton("Go forward");
b1.setFont(new Font("宋体", Font.PLAIN, 30));
b1.setForeground(Color.black);
b1.setBackground(Color.white);
frame.getContentPane().add(b1,BorderLayout.NORTH);
//将按钮b1添加人最底层布局BorderLayout的背部

JPanel p=new JPanel();
p.setLayout(new FlowLayout(1,30,30));
//设置新的网格布局FlowLayout

JButton b2=new JButton("确定发送指令");
b2.setFont(new Font("宋体", Font.PLAIN, 20));
b2.setForeground(Color.black);
b2.setBackground(Color.white);
//设置按钮b2的字体大小和颜色
p.add(b2);
//将b2添加入P布局中

JButton b3=new JButton("取消该指令");
b2.setFont(new Font("宋体", Font.PLAIN, 20));
b2.setForeground(Color.black);
b2.setBackground(Color.white);
p.add(b3);


frame.getContentPane().add(p,BorderLayout.SOUTH);
frame.setBounds(300, 200, 450, 300);
//设置窗口初设位置和宽高
frame.getLayeredPane().setLayout(null);
//把背景图片添加到分层窗格的最底层作为背景#
frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//窗口关闭操作
frame.setSize(background.getIconWidth(), background.getIconHeight());
frame.setResizable(false);
//设置窗口大小不可变
frame.setVisible(true);

}
}


运行截图:









猜你喜欢

转载自blog.csdn.net/congcong7267/article/details/79252406