JPanel显示不出来,大虾请帮忙!先谢了!

happy_yls 2007-11-27 04:50:02
初学GUI,想在一个Frame上放多个Panel,左右各一个,就像windows资源管理器那样把窗口分成左右两部分,但大小可以自己控制,
MyPanel上都已放了控件
private void initialize() {
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(new MyPanel(),BorderLayout.WEST);
this.getContentPane().add(new MyPane1(),BorderLayout.EAST);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setForeground(Color.white);
this.setBackground(Color.white);
this.setResizable(true);
this.setSize(new Dimension(800, 600));
this.setTitle("演示程序");
}
如上代码,怎么是一个空白窗口?只有将以上红色部分代码其中一个改为
this.getContentPane().add(new MyPanel(),BorderLayout.CENTER);
才可以显示出来,是不是必须有一个panel要放中间???怎样做才能实现我的需求呢?
...全文
310 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
huadis 2007-11-30
  • 打赏
  • 举报
回复

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;

import java.awt.Color;

public class MyFrame extends JFrame {

private static final long serialVersionUID = 1L;

/**
* This is the default constructor
*/
public MyFrame() {
super();
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(new MyPanel("aaaaa"),BorderLayout.WEST);
this.getContentPane().add(new MyPanel("bbbbb"),BorderLayout.CENTER);
//this.getContentPane().add(new AAA(),BorderLayout.CENTER);
//this.getContentPane().add(myTabbedPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setForeground(Color.white);
this.setBackground(Color.white);
this.setResizable(true);
this.setSize(new Dimension(800, 600));
this.setTitle("演示程序");
show();
}

/**
* This method initializes this
*
* @return void
*/

public static void main(String args[]){
MyFrame f=new MyFrame();
}

} // @jve:decl-index=0:visual-constraint="10,10"







import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import java.awt.*;

class MyPanel extends JPanel {//根据当前时间画一个坐标轴


/**
* This is the default constructor
*/
public MyPanel(String s) {
JLabel lb=new JLabel(s);
lb.setBounds(50,20,300,100);
add(lb);
Border border=BorderFactory.createBevelBorder(BevelBorder.RAISED,new Color(122,138,153),new Color(54,71,89));
setBorder(border);
}
}



估计不合你要求.
happy_yls 2007-11-28
  • 打赏
  • 举报
回复
试了一下,如果改为
this.getContentPane().add(new JButton(),BorderLayout.WEST);
this.getContentPane().add(new JButton(),BorderLayout.CENTER);
可以显示,是不是frame上不可以放多个panel啊?
happy_yls 2007-11-28
  • 打赏
  • 举报
回复
今天又试了,还是只能看到Center的panel,west的panle看不到,现在把代码全贴出来:
package happy;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;

import java.awt.Color;

public class MyFrame extends JFrame {

private static final long serialVersionUID = 1L;

/**
* This is the default constructor
*/
public MyFrame() {
super();
initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(new MyPanel(),BorderLayout.WEST);
this.getContentPane().add(new MyPanel(),BorderLayout.CENTER);
//this.getContentPane().add(new AAA(),BorderLayout.CENTER);
//this.getContentPane().add(myTabbedPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setForeground(Color.white);
this.setBackground(Color.white);
this.setResizable(true);
this.setSize(new Dimension(800, 600));
this.setTitle("演示程序");
}


} // @jve:decl-index=0:visual-constraint="10,10"

******************************************************
package happy;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import javax.swing.JPanel;
import java.awt.Font;
import java.awt.Color;

public class MyPanel extends JPanel {//根据当前时间画一个坐标轴

private static final long serialVersionUID = 1L;

static final int count = 12;// 1个时间轴5个候车室

static final int hour = 3;// 显示未来3个小时

static final int minute = 10;// 10分钟一小格,刷新时间也为10分钟

private float width = 0;// 每个显示单位的宽度高度

private float heigth = 0;

private float screenWidth = 0;// 屏幕的宽度高度,实际是thisPanel的宽度高度

private float screenHeigth = 0;

private Point2D pointBegin;

private Point2D pointEndx;

private Point2D pointEndy;

/**
* This is the default constructor
*/
public MyPanel() {
super();
initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setLayout(new BorderLayout());
this.setFont(new Font("Arial", Font.BOLD, 12));
this.setBackground(Color.white);
}

protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
screenWidth = this.getWidth();
screenHeigth = this.getHeight();// 得到Panel高度
width = screenWidth / 2;// 得到Panel宽度,一半作为x线段宽度
heigth = screenHeigth / count ;// 得到每个图象高度

ArrayList al = Tools.getFutureTime(hour, minute);
for (int m = 0; m < count; m++) {
pointBegin = new Point2D.Double((screenWidth - width) / 2, heigth
* 9 / 10 + m * heigth);
pointEndx = new Point2D.Double((screenWidth + width) / 2, heigth
* 9 / 10 + m * heigth);
pointEndy = new Point2D.Double((screenWidth - width) / 2, heigth
* 1 / 10 + m * heigth);
Line2D linex = new Line2D.Double(pointBegin, pointEndx);
Line2D liney = new Line2D.Double(pointBegin, pointEndy);
g2d.draw(liney);// Y轴
g2d.draw(linex);// X轴
// *********************时间刻度*********************************
int division = hour * (60 / minute);
if (m == 0) {
for (int i = 0; i < division; i++) {
float length = heigth * 9 / 10 + 6;
String time = al.get(i).toString();
Point2D pointxx = new Point2D.Double((screenWidth - width)
/ 2 + width * i / division, heigth * 9 / 10 + m
* heigth);
if (time.indexOf(":00") > -1 || time.indexOf(":30") > -1)
length = heigth * 9 / 10 + 12;
Point2D pointxy = new Point2D.Double((screenWidth - width)
/ 2 + width * i / division, length + m * heigth);
Line2D linexx = new Line2D.Double(pointxx, pointxy);
g2d.draw(linexx);
if (time.indexOf(":00") > -1)
g2d.drawString(al.get(i).toString().substring(0, 2),
(screenWidth - width) / 2
+ (screenWidth - width) * i / division,
length + m * heigth);
}
}
}

// ***********************************************************
}
}


lihaifeng0412 2007-11-27
  • 打赏
  • 举报
回复
不可能把,把你的代码都帖出来
zjucoco 2007-11-27
  • 打赏
  • 举报
回复
this.getContentPane().add(new MyPane1(),BorderLayout.EAST);
这行你是不是把l写成1了?

还有,为了布局好看一般都是一个是West,一个是Center。或者,一个是East,一个是Center。

62,623

社区成员

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

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