当前位置: 代码迷 >> J2SE >> java 图形显示有关问题(setVisible和repaint有关问题)
  详细解决方案

java 图形显示有关问题(setVisible和repaint有关问题)

热度:14   发布时间:2016-04-23 21:56:43.0
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());
  相关解决方案