java 图形显示问题(setVisible和repaint问题)
本帖最后由 fdcumt 于 2013-06-24 11:02:28 编辑
setvisible放置位置对显示jframe的影响,我想知道这是为什么?
package frameDemo;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;
public class calculator {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
calculator c=new calculator();
}
private JFrame _calculateJFrame;
private JButton _mutilJButten,_DivJButton,_AddJButton,_subJButton;
private JTextField jTextFileFirst,jTextFileSecond,jTextFileLast;
private JPanel jPanelFirst,jPanelSecond,jPanelThird,jPanelFour;
public calculator(){
//_calculateJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_calculateJFrame=new JFrame("Calculator");
_calculateJFrame.setLayout(new GridLayout(4,1));
_calculateJFrame.setBounds(40, 30, 400, 380);
_calculateJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//注意这行代码,如果放到最后不会显示在画布上添加的东西
_calculateJFrame.setVisible(true);
//添加四个画布
jPanelFirst = new JPanel();
jPanelSecond=new JPanel();
jPanelThird=new JPanel();
jPanelFour=new JPanel();
_calculateJFrame.add(jPanelFirst);
_calculateJFrame.add(jPanelSecond);
_calculateJFrame.add(jPanelThird);
_calculateJFrame.add(jPanelFour);
//_calculateJFrame.setVisible(true);
jPanelFirst.setLayout(null);
jPanelFirst.setBackground(Color.yellow);
jTextFileFirst=new JTextField();
jPanelFirst.add(jTextFileFirst);
jTextFileFirst.setSize( jPanelFirst.getWidth(),jPanelFirst.getHeight());
//jTextFileFirst.setSize(100, 100);
//jTextFileFirst.setBackground(Color.black);
jPanelSecond.setLayout(null);
jPanelSecond.setBackground(Color.red);
jTextFileSecond=new JTextField();
jTextFileSecond.setSize( jPanelSecond.getWidth(),jPanelSecond.getHeight());
jPanelSecond.add(jTextFileSecond);
jPanelThird.setLayout(null);
jPanelThird.setBackground(Color.blue);
//添加+ - * /
_AddJButton=new JButton("+");
jPanelThird.add(_AddJButton);
//_AddJButton.setBounds(jPanelThird.getLocation().x,jPanelThird.getLocation().y,jPanelThird.getWidth()/4,jPanelThird.getHeight());
_AddJButton.setBounds(0,0,jPanelThird.getWidth()/4,jPanelThird.getHeight());
//_AddJButton.setBackground(Color.black);
_subJButton=new JButton("-");
jPanelThird.add(_subJButton);
_subJButton.setBounds(_AddJButton.getWidth(),0,jPanelThird.getWidth()/4,jPanelThird.getHeight());
_mutilJButten=new JButton("*");
_mutilJButten.setBounds(_AddJButton.getWidth()*2,0,jPanelThird.getWidth()/4,jPanelThird.getHeight());
jPanelThird.add(_mutilJButten);
_DivJButton=new JButton("/");
_DivJButton.setBounds(jPanelThird.getWidth()/4*3,0,jPanelThird.getWidth()/4,jPanelThird.getHeight());
jPanelThird.add(_DivJButton);
jPanelFour.setLayout(null);
jPanelFour.setBackground(Color.green);
jTextFileLast=new JTextField();
jTextFileLast.setSize(jPanelSecond.getWidth(), jPanelSecond.getHeight());
jPanelFour.add(jTextFileLast);
//不用repaint方法也可以显示panel上的东西
//_calculateJFrame.repaint();
}
}
package myMenu;
import java.awt.Button;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
public class myMeny {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
myMeny firstMenu=new myMeny();
}
private JFrame _MenuFrame;
private JMenuBar _firstMenuBar;
private JMenu _architectureMenu,_computerMenu,_helpMenu;
public myMeny() {
_MenuFrame=new JFrame("选课系统");
_MenuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//如果把setvisible写在这就只会显示一部分,但是写在最后就全部显示
//_MenuFrame.setVisible(true);
_MenuFrame.setSize(300,400);
_MenuFrame.setLocationRelativeTo(null);
_firstMenuBar=new JMenuBar();
_MenuFrame.setJMenuBar(_firstMenuBar);
_architectureMenu=new JMenu("文体类");
_firstMenuBar.add(_architectureMenu);
JMenuItem _arcItem=new JMenuItem("艺术欣赏");
JMenuItem _bodyBuildingiItem=new JMenuItem("健美操");
JMenuItem _basketballItem=new JMenuItem("篮球");
_architectureMenu.add(_arcItem);
_architectureMenu.add(_bodyBuildingiItem);
_architectureMenu.add(_basketballItem);
_MenuFrame.repaint();
_computerMenu=new JMenu("计算机类");
_firstMenuBar.add(_computerMenu);
_computerMenu.add(new JMenuItem("JAVA语言"));
_computerMenu.add(new JMenuItem("网络编程"));
_helpMenu=new JMenu("帮助");
_firstMenuBar.add(_helpMenu);
_helpMenu.add(new JMenuItem("选课方法"));
//如果在上面使用setvisible方法,这里使用repaint也不能显示全部,但是最小化再最大
//化之后就可以显示全部
//_MenuFrame.repaint();
_MenuFrame.setVisible(true);
}
}
((JComponent)_MenuFrame.getContentPane()).revalidate();
((JComponent)_MenuFrame.getContentPane()).repaint();