1.一些基本组件的应用
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class frame {
public static void main(String[] args)
{
JFrame frame=new JFrame("I'm a JFrame~");
frame.getContentPane().setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
Dimension displaySize=Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((displaySize.width-frame.getSize().width)/2,(displaySize.height-frame.getSize().height)/2);
//jlabel
JLabel label=new JLabel("Hello,java~",JLabel.CENTER);
label.setVerticalTextPosition(JLabel.BOTTOM);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setBounds(200,10,100,300);
ImageIcon ico = new ImageIcon("src/pic/1.png");
//Jicon ico=new
label.setIcon(ico);
frame.add(label);
//jbutton
JButton button=new JButton(new ImageIcon("src/pic/2.png"));
button.setRolloverIcon(new ImageIcon("src/pic/3.png"));
button.setPressedIcon(new ImageIcon("src/pic/4.png"));
button.setBounds(10,10,150,150);
button.setMargin(new Insets(10,10,10,10));
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("ActionEvent~");
}
});
button.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
System.out.println("ChangeEvent~");
}
});
frame.add(button);
//button.setPreferredSize(11,22);
//jradiobutton
JRadioButton manRadioButton = new JRadioButton("xx",true);
JRadioButton womanRadioButton = new JRadioButton("xy",false);
ButtonGroup sexRadioButtonGroup=new ButtonGroup();
sexRadioButtonGroup.add(manRadioButton);
sexRadioButtonGroup.add(womanRadioButton);
manRadioButton.setBounds(300,10,50,50);
womanRadioButton.setBounds(350,10,50,50);
frame.add(manRadioButton);
frame.add(womanRadioButton);
//jlist
String[] weekends={"Monday","Tusday","Wednesday","Thursday","Friday","Saturday","Sunday"};
JList weekList=new JList(weekends);
weekList.setFixedCellHeight(20);
weekList.setFixedCellWidth(5);
weekList.setSelectionBackground(Color.blue);
weekList.setSelectionForeground(Color.gray);
//weekList.setBounds(400,10,110,110);
JScrollPane scroll=new JScrollPane(weekList);
int Height=(int)weekList.getPreferredScrollableViewportSize().getHeight();
scroll.setBounds(400,10,110,80);
frame.add(scroll);
; frame.getRootPane().setDefaultButton(button);
frame.setVisible(true);
}
}
2.布局组件
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class borderLayout extends JFrame{
private JButton upButton;
private JButton downButton;
private JButton leftButton;
private JButton rightButton;
private JButton centerButton;
public borderLayout()
{
setTitle("BorderLayout~");
this.setBounds(100,100,300,400);
upButton=new JButton("up");
downButton=new JButton("down");
leftButton=new JButton("left");
rightButton=new JButton("right");
centerButton=new JButton("center");
Container panel=this.getContentPane();
panel.setLayout(new BorderLayout());
panel.add(upButton,BorderLayout.NORTH);
panel.add(downButton,BorderLayout.SOUTH);
panel.add(leftButton,BorderLayout.WEST);
panel.add(rightButton,BorderLayout.EAST);
panel.add(centerButton,BorderLayout.CENTER);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
borderLayout demo=new borderLayout();
demo.setVisible(true);
System.out.println("Hello,java!");
}
}