javaswing例⼦(⼀些)
码码;
package swing1;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class EmptyJFrame extends JFrame{
public static void main(String[] args) {
//现在创建了⼀个对象,不过什么都显⽰不出来
EmptyJFrame f = new EmptyJFrame();
//加上这⼀句就可以显⽰⼀个仅有关闭,最⼩化,最⼤化的按钮的Frame了
f.setVisible(true);
/
/再加上这⼀句就可以显⽰⼀个在左上⾓,拥有指定⼤⼩的Frame了
f.setSize(300,200);
//再加上这⼀句就可以把Frame放在最中间了
f.setLocationRelativeTo(null);
//如果没有这⼀句,在点击关闭Frame的时候程序其实还是在执⾏状态中的,加上这⼀句才算是真正的把资源释放掉了 f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
package swing1;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class EmptyJFrame2 extends JFrame{
EmptyJFrame2(){
initGUI();
}
private void initGUI(){
setVisible(true);
setSize(300,200);
javaswing实现购买setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
EmptyJFrame2 f = new EmptyJFrame2();
}
});
}
}
图图;
码码;
package swing1;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class AddComponent extends JFrame{
public AddComponent(){
initGUI();
}
private void initGUI(){
setVisible(true);
setSize(300,200);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//创建元件
JButton jButton1 = new JButton("jButton1");
//添加元件
add(jButton1);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
AddComponent f = new AddComponent();
}
});
}
}
package swing1;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class AddComponent2 extends JFrame{
//定义变量
private JButton jButton1;
public AddComponent2(){
initGUI();
addComp();
}
private void addComp(){
//初始化
jButton1 = new JButton("jButton1");
//添加元件
add(jButton1);
}
private void initGUI(){
setVisible(true);
setSize(300,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); }
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
AddComponent2 f = new AddComponent2();
}
});
}
}
图图;
码码;
package swing1;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
/**
* swing基础实例
* @author HZ20232
* @author HZ20232
*
*/
public class Hello{
public static void main(String args[])throws Exception{
NewFrame frame1 = new NewFrame();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//⼀定要设置关闭
frame1.setVisible(true);
}
}
class NewFrame extends JFrame{
private JLabel label1;
private JButton button1;
private JTextField text1;
private JComboBox box;
private JMenuBar menuBar;
private JSlider slider;
private JSpinner spinner;
private JToolBar toolBar;
public NewFrame(){
super();
this.setSize(300,500);
// ContentPane().setLayout(new FlowLayout());//设置布局控制器
// ContentPane().setLayout(new GridLayout(1,2));//设置布局控制器,需要给出设定的⾏列数⽬
// ContentPane().setLayout(new BorderLayout());//设置布局控制器,以North,South,West,East,来控制控件布局// ContentPane().setLayout(new GridBagLayout
());//设置布局控制器
this.TextField(),null);//添加⽂本框
this.Button(),null);//添加按钮
this.Label(),null);//添加标签
this.Box(),null);//添加下拉列表框
this.Menu());//添加菜单
this.Slider(),null);
this.Spinner(),null);
this.ToolBar(),null);
this.setTitle("Hello World!");//设置窗⼝标题
}
private JToolBar getToolBar(){
if(toolBar==null){
toolBar = new JToolBar();
toolBar.setBounds(103,260,71,20);
toolBar.setFloatable(true);
}
return toolBar;
}
private JSpinner getSpinner(){
if(spinner==null){
spinner = new JSpinner();
spinner.setBounds(103,220, 80,20);
spinner.setValue(100);
}
return spinner;
}
}
private JSlider getSlider(){
if(slider==null){
slider = new JSlider();
slider.setBounds(103,200,100, 20);
slider.setMaximum(100);
slider.setMinimum(0);
slider.setOrientation(0);
slider.setValue(0);
}
return slider;
}
/**
* 菜单的级别JMenuBar->JMenu->JMenuItem * 三级都是1:n的关系
* 最后添加菜单⽤的SetJMenuBar⽅法
* @return 建⽴好的菜单
*/
private JMenuBar getMenu(){
if(menuBar==null){
menuBar = new JMenuBar();
JMenu m1 = new JMenu();
m1.setText("⽂件");
JMenu m2 = new JMenu();
m2.setText("编辑");
JMenu m3 = new JMenu();
m3.setText("帮助");
JMenuItem item11 = new JMenuItem(); item11.setText("打开");
JMenuItem item12 = new JMenuItem(); item12.setText("保存");
JMenuItem item13 = new JMenuItem(); item13.setText("退出");
JMenuItem item21 = new JMenuItem(); item21.setText("复制");
JMenuItem item22 = new JMenuItem(); item22.setText("拷贝");
JMenuItem item23 = new JMenuItem(); item23.setText("剪切");
JMenuItem item31 = new JMenuItem(); item31.setText("欢迎");
JMenuItem item32 = new JMenuItem(); item32.setText("搜索");
JMenuItem item33 = new JMenuItem(); item33.setText("版本信息");
m1.add(item11);
m1.add(item12);
m1.add(item13);
m2.add(item21);
m2.add(item22);
m2.add(item23);
m3.add(item31);
m3.add(item32);
m3.add(item33);
menuBar.add(m1);
menuBar.add(m2);
menuBar.add(m3);
}
return menuBar;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论