18グラフィックインタフェース
26781 ワード
グラフィックインタフェース
public static void main(String[] args) {
JFrame frame = new JFrame(" ");
//
// frame.setSize(300,400);
// ( )
//frame.setBounds((1366-300)/2, (768-400)/2, 300,400); // x , y 。 , 。
initFrame(frame, 300,400);
frame.setVisible(true); //setVisible 。
//
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 。
public static void initFrame(JFrame frame,int width , int height){
Toolkit toolkit = Toolkit.getDefaultToolkit(); //
//
Dimension d = toolkit.getScreenSize();
int x = (int) d.getWidth();
int y = (int) d.getHeight();
frame.setBounds((x-width)/2, (y-height)/2, width, height);
}
ダイアログクラス(Dialog):
public static void main(String[] args) {
/*JFrame frame = new JFrame(" ");
//
JDialog dialog = new JDialog(frame, " ",true);
//
FrameUtil.initFrame(frame, 300, 400);
dialog.setBounds(500,300, 100, 200);
dialog.setVisible(true); //
*/
JFrame frame = new JFrame(" ");
//
FrameUtil.initFrame(frame, 300, 400);
//
/*JOptionPane.showMessageDialog(frame, " ", " ",JOptionPane.INFORMATION_MESSAGE);
//
JOptionPane.showMessageDialog(frame," "," ",JOptionPane.WARNING_MESSAGE);*/
//
// JOptionPane.showMessageDialog(frame," 6 "," ",JOptionPane.ERROR_MESSAGE);
//
/* String moeny = JOptionPane.showInputDialog(" ($)");
System.out.println("money:"+ moeny);*/
//
int num = JOptionPane.showConfirmDialog(frame, " ?");
System.out.println(num);
public static void main(String[] args) {
JFrame frame = new JFrame(" ");
// ( )
//FileDialog fileDialog = new FileDialog(frame, " ", FileDialog.LOAD);
FileDialog fileDialog = new FileDialog(frame," ",FileDialog.SAVE);
FrameUtil.initFrame(frame, 300,400);
fileDialog.setVisible(true);
System.out.println(" :"+ fileDialog.getDirectory());
System.out.println(" :"+ fileDialog.getFile());
}
public static void main(String[] args) {
JFrame frame = new JFrame(" ");
//
JPanel panel = new JPanel();
//
panel.setBackground(Color.RED);
//
frame.add(panel);
FrameUtil.initFrame(frame, 400, 300);
}
非コンテナコンポーネント
public static void main(String[] args) {
JFrame frame= new JFrame(" ");
//
JPanel panel = new JPanel();
frame.add(panel);
//
JLabel nameLabel = new JLabel(" ");
//
JTextField nameField = new JTextField(12);
//
panel.add(nameLabel);
panel.add(nameField);
//
JLabel passLabel= new JLabel(" ");
//
JPasswordField passField = new JPasswordField(12);
//
panel.add(passLabel);
panel.add(passField);
// --
JLabel sexLabel = new JLabel(" ");
JRadioButton man = new JRadioButton(" ",true);
JRadioButton woman = new JRadioButton(" ");
// ,
ButtonGroup group = new ButtonGroup();
group.add(woman);
group.add(man);
//
panel.add(sexLabel);
panel.add(man);
panel.add(woman);
// --->
JLabel cityLabel = new JLabel(" ");
Object[] arr = {" "," "," "," "," "};
JComboBox citys = new JComboBox(arr);
panel.add(cityLabel);
panel.add(citys);
// ---->
JLabel hobitLabel = new JLabel(" :");
JCheckBox checkBox1 = new JCheckBox(" ",true);
JCheckBox checkBox2 = new JCheckBox("java",true);
JCheckBox checkBox3 = new JCheckBox("javascript");
JCheckBox checkBox4 = new JCheckBox("android");
panel.add(hobitLabel);
panel.add(checkBox1);
panel.add(checkBox2);
panel.add(checkBox3);
panel.add(checkBox4);
//
JLabel jLabel = new JLabel(" ");
JTextArea area = new JTextArea(20, 15);
area.setLineWrap(true); //
panel.add(jLabel);
panel.add(area);
FrameUtil.initFrame(frame, 500, 400);
}
メニューコンポーネント
JFrame frame = new JFrame(" ");
//
JMenuBar bar = new JMenuBar();
//
JMenu fileMenu = new JMenu(" ");
JMenu editMenu = new JMenu(" ");
JMenu switchMenu = new JMenu(" ");
//
JMenuItem openMenu = new JMenuItem(" ");
JMenuItem saveMenu = new JMenuItem(" ");
JMenuItem aboutMenu = new JMenuItem(" ");
JMenuItem closeMenu = new JMenuItem(" ");
JMenuItem workMenu1 = new JMenuItem("0910project");
JMenuItem workMenu2 = new JMenuItem("1208project");
JMenuItem workMenu3 = new JMenuItem("1110project");
JTextArea area = new JTextArea(20,30);
public void initNotepad(){
//
fileMenu.add(openMenu);
fileMenu.add(saveMenu);
editMenu.add(aboutMenu);
editMenu.add(closeMenu);
//
switchMenu.add(workMenu1);
switchMenu.add(workMenu2);
switchMenu.add(workMenu3);
//
fileMenu.add(switchMenu);
//
bar.add(fileMenu);
bar.add(editMenu);
//
frame.add(bar,BorderLayout.NORTH);
frame.add(area);
FrameUtil.initFrame(frame, 500, 600);
}
public static void main(String[] args) {
new Demo2().initNotepad();
}
レイアウトマネージャ
public static void main(String[] args) {
JFrame frame = new JFrame(" ");
//
BorderLayout borderLayout = new BorderLayout();
// borderlayout frame 。
frame.setLayout(borderLayout);
frame.add(new JButton(" "),BorderLayout.NORTH);
frame.add(new JButton(" "),BorderLayout.SOUTH);
frame.add(new JButton(" "),BorderLayout.WEST);
frame.add(new JButton(" "),BorderLayout.EAST);
frame.add(new JButton(" "),BorderLayout.CENTER);
//
FrameUtil.initFrame(frame, 300, 300);
を使用public static void main(String[] args) {
JFrame frame = new JFrame(" ");
//
JPanel panel = new JPanel();
frame.add(panel);
//
FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT, 0, 30);// FlowLayout.LEFT 。
// frame
panel.setLayout(flowLayout);
panel.add(new JButton(" 1"));
panel.add(new JButton(" 2"));
panel.add(new JButton(" 3"));
panel.add(new JButton(" 4"));
//
FrameUtil.initFrame(frame, 300, 300);
}
public static void main(String[] args) {
JFrame frame = new JFrame(" ");
//
GridLayout gridLayout = new GridLayout(4, 4, 1, 2);
//
frame.setLayout(gridLayout);
for(int i = 0 ; i<10; i++){
frame.add(new JButton(i+""));
}
frame.add(new JButton("+"));
frame.add(new JButton("-"));
frame.add(new JButton("*"));
frame.add(new JButton("/"));
frame.add(new JButton("="));
frame.add(new JButton("."));
// frame.add(new JButton("aa"));
//
FrameUtil.initFrame(frame, 300, 300);
}
public static void main(String[] args) {
JFrame frame = new JFrame(" ");
final JPanel panel = new JPanel();
frame.add(panel);
//
final CardLayout cardLayout = new CardLayout();
panel.setLayout(cardLayout);
//
JButton button = new JButton(" A");
panel.add(button);
panel.add(new JButton(" K"));
panel.add(new JButton(" 6"));
panel.add(new JButton(" 2"));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.next(panel); //
// cardLayout.previous(parent);
}
});
//
FrameUtil.initFrame(frame,300, 300);
}
public static void main(String[] args) {
JFrame frame = new JFrame(" ");
JButton button = new JButton(" ");
frame.add(button);
// 。
button.addActionListener(new ActionListener() {
// , actionPerformed 。
@Override
public void actionPerformed(ActionEvent e) { // ActionEvent ,jvm ActionEvent, actionPerformed 。
//System.out.println(" , ...");
JButton button =(JButton) e.getSource(); //getSource()
if(button.getText().equals(" ")){
button.setText(" ");
}else{
button.setText(" ");
}
}
});
public static void main(String[] args) {
JFrame frame = new JFrame(" ");
JButton button = new JButton(" ");
frame.add(button);
//
/*button.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
System.out.println(" ...");
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println(" ..");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println(" ...");
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println(" ...");
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(" ..");
}
});
, ??
: 。 MouseListener , 。
*/
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// System.out.println(" ..");
if(e.getClickCount()==2){
System.out.println(" ..");
}
}
});
FrameUtil.initFrame(frame, 300, 300);
}
public static void main(String[] args) {
JFrame frame = new JFrame(" ");
JButton button = new JButton(" ");
frame.add(button);
//
/*button.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { System.out.println(" "); } @Override public void keyReleased(KeyEvent e) { // System.out.println(" "); } @Override public void keyPressed(KeyEvent e) { System.out.println(" .."); } });*/
button.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// System.out.println(" :"+e.getKeyChar());
//System.out.println(" :"+ e.getKeyCode());
int code = e.getKeyCode();
switch (code) {
case 38:
System.out.println(" ");
break;
case 40:
System.out.println(" ");
break;
case 37:
System.out.println(" ");
break;
case 39:
System.out.println(" ");
break;
default:
break;
}
}
});
FrameUtil.initFrame(frame,300, 300);
}