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

fdcumt 2013-06-24 11:00:25

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);






}









}
...全文
423 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fdcumt 2013-07-25
  • 打赏
  • 举报
回复
一个setvisible放在前面正确显示,一个放在后面正确显示,huntor说必须放在后面???明显和结果不一致嘛。。。。。。。
raistlic 2013-07-25
  • 打赏
  • 举报
回复
Swing 组件同时是容器,有一个状态叫做是否 "valid" 即其内部子控件有添加、删除等操作,或者其布局产生任何变化后,有没有重新算好所有子控件的大小和位置 setVisible(true) 可以先写,后面只要这样调用即可:

((JComponent)_MenuFrame.getContentPane()).revalidate();
((JComponent)_MenuFrame.getContentPane()).repaint();
StevenLoveMaggie 2013-07-01
  • 打赏
  • 举报
回复
setVisible()与放置的位置没有关系。在api中,

我们可以知道,其作用就是为了显示窗体。
GUI程序是时间驱动的,不是顺序执行的。当最大化/最小化就是事件,GUI会重新加载组件,所以此时看见的就是完整的。
老帅哥23333 2013-06-30
  • 打赏
  • 举报
回复
看一下我的这个程序
import java.awt.Frame;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.BorderLayout;

class TestFrameProblem
{
	JFrame container=new JFrame();

	/**/
	 JLabel title ;
	 JPanel centerContainer;
	 JButton click;
	 JLabel statusBar;

	public TestFrameProblem()
	{
		
		container.setSize(300,500);
		container.setVisible(true);

		title=new JLabel("edit here");
		click=new JButton("click here");

		centerContainer=new JPanel();
		centerContainer.add(click);
		statusBar=new JLabel("status is in here");
	
		container.add(title,BorderLayout.NORTH);
		container.add(centerContainer,BorderLayout.CENTER);
		container.add(statusBar,BorderLayout.SOUTH);

	}
	
	public static void main(String[] args) 
	{
		new TestFrameProblem();
	}
}
你运行一下。看看
摆烂办不到 2013-06-30
  • 打赏
  • 举报
回复
引用 1 楼 huntor 的回复:
最后调用setVisible(true)。 组件还没添加就显示出来,明显是错误的做法。
赞一个!
地下室森林 2013-06-29
  • 打赏
  • 举报
回复
setvisible要放在最后面。就好比楼房要建好才能住人
折柳君 2013-06-29
  • 打赏
  • 举报
回复
可能与你设置的尺寸有关系的呢!有的时候你设置的尺寸大小小啦!_MenuFrame.setSize(300,400);改大一点再试试啊!
fdcumt 2013-06-25
  • 打赏
  • 举报
回复
第一个程序必须先调用setvisible再添加组件,这又是什么原因???
huntor 2013-06-24
  • 打赏
  • 举报
回复
最后调用setVisible(true)。 组件还没添加就显示出来,明显是错误的做法。
Java绘制不规则几何图形,比如划曲线,写字,线条随意画,如截图所示,甚至可以写出文字:   不规则图形的绘制代码:   public class IrregularShapeDemo extends JFrame {    GeneralPath gPath= new GeneralPath(); //GeneralPath对象实例   //构造函数   public IrregularShapeDemo() {    super("不规则图形的绘制"); //调用父类构造函数    enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允许事件    setSize(300, 200); //设置窗口尺寸    setVisible(true); //设置窗口可视    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序    }    public void paint(Graphics g) { //重载窗口组件的paint()方法    Graphics2D g2D = (Graphics2D)g; //获取图形环境    g2D.draw(gPath); //绘制路径    }    public static void main(String[] args) {    new IrregularShapeDemo();    }    protected void processMouseEvent(MouseEvent e) { //鼠标事件处理    if(e.getID() == MouseEvent.MOUSE_PRESSED) {    aPoint = e.getPoint(); //得到当前鼠标点    gPath = new GeneralPath(); //重新实例化GeneralPath对象    gPath.moveTo(aPoint.x,aPoint.y); //设置路径点    }    }    protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理    if(e.getID() == MouseEvent.MOUSE_DRAGGED) {    aPoint = e.getPoint(); //得到当前鼠标点    gPath.lineTo(aPoint.x, aPoint.y); //设置路径    gPath.moveTo(aPoint.x, aPoint.y);    repaint(); //重绘组件    }    }   }
Java在窗口上加载显示GIF动画图像,将多个独立的GIF图像串联在一起显示,形成GIF特有的动画形式。主要代码如下:   ImageIcon[] images; //用于动画的图标数组   Timer animationTimer;   int currentImage = 0; //当前图像编号   int delay = 500; //图像切换延迟   int width; //图像宽度   int height; //图像高度   public AnimatorIcon() //构造函数   {    setBackground(Color.white);    images = new ImageIcon[2]; //初始化数组    for (int i=0;i   images[i]=new ImageIcon(getClass().getResource("image" i ".gif")); //实例化图标    width = images[0].getIconWidth(); //初始化宽度值    height = images[0].getIconHeight(); //初始化高度值   }   public void paintComponent(Graphics g) { //重载组件绘制方法    super.paintComponent(g); //调用父类函数    images[currentImage].paintIcon(this,g,70,0); //绘制图标    currentImage=(currentImage 1)%2; //更改当前图像编号   }   public void actionPerformed(ActionEvent actionEvent) {    repaint();   }   public void startAnimation() { //开始动画    if (animationTimer==null) {    currentImage=0;    animationTimer=new Timer(delay, this); //实例化Timer对象    animationTimer.start(); //开始运行    } else if (!animationTimer.isRunning()) //如果没有运行    animationTimer.restart(); //重新运行   }   public void stopAnimation() {    animationTimer.stop(); //停止动画   }   public static void main(String args[]) {    AnimatorIcon animation = new AnimatorIcon(); //实例化动画图标    JFrame frame = new JFrame("动画图标"); //实例化窗口对象    frame.getContentPane().add(animation); //增加组件到窗口上    frame.setSize(200, 100); //设置窗口尺寸    frame.setVisible(true); //设置窗口可视    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序    animation.startAnimation(); //开始动画

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧