谢谢
解决方案 »
- 上传附件的问题 下面的函数是判断附件是否存在,但是走到objStream.LoadFromFile(strFileName);退到catch里,在我的机器上是好用的,但是
- 伤心欲绝,再问一贴
- java中有没有这样一个方法,就是当我的TEXTAREA的内容发生改变时触发一个事件
- 关于动画的问题
- java这么多方向,该选哪个?
- Jlist 怎么多行显示一条文本信息,譬如超过20个字符了,自动换行,但还是在一个Item里
- 问题标签用户 java swing JInternalFrame的焦点问题
- 请问。。。
- 学习java前, 最好先学点什么语言基础,会有助于学习java?
- new 出问题
- close的问题,JDBC和IO操作的区别?
- Charset 和 Character set 有啥異同?
所以JLable不行的
Swing和AWT的部件在Z-Order的布置上有出人,所以你的JMenu显示不出来。
import java.awt.*;
import java.awt.event.*;public class Score extends JFrame implements ActionListener { /**
* @param args
*/
jpanel[] j;
Container contentPane;
int Count;
Score(String title)
{
super(title);
contentPane=this.getContentPane();
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
addPane(6);
initMenu();
}
void addPane(int n)
{
j=new jpanel[n];
Count=n;
for(int i=0;i<n;i++)
{
j[i]=new jpanel();
contentPane.add(j[i]);
}
}
void initMenu()
{
JMenuBar mb=new JMenuBar();
JMenu jmStart=new JMenu("开始");
JMenuItem jmiStart=new JMenuItem("开始");
JMenuItem jmiEnd =new JMenuItem("结束");
JMenuItem jmiExit =new JMenuItem("退出");
jmStart.add(jmiStart);
jmStart.add(jmiEnd);
jmStart.add(jmiExit);
jmiStart.addActionListener(this);
jmiEnd.addActionListener(this);
jmiExit.addActionListener(this);
JMenu jmAdd=new JMenu("增加候选人");
JMenuItem jmiAdd=new JMenuItem("增加");
JMenuItem jmiScore=new JMenuItem("得分");
jmAdd.add(jmiAdd);
jmAdd.add(jmiScore);
jmiAdd.addActionListener(this);
jmiScore.addActionListener(this);
mb.add(jmStart);
mb.add(jmAdd);
setJMenuBar(mb);
}
public void actionPerformed(ActionEvent e)
{
JMenuItem jmi=(JMenuItem)e.getSource();
if(jmi.getActionCommand().equals("退出"))
{
System.exit(0);
}
else if(jmi.getActionCommand().equals("增加"))
{ }
}
public static void main(String[] args) {
// TODO 自动生成方法存根
final Score s=new Score("演讲记分系统");
s.setBounds(100,100,800,400);
//s.setExtendedState(s.MAXIMIZED_BOTH);
s.setDefaultCloseOperation(s.DISPOSE_ON_CLOSE);
s.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
});
s.setVisible(true);
}}class jpanel extends JPanel implements ActionListener
{
JButton jb;
Rectangle rect;
jpanel()
{
this.setBackground(Color.WHITE);
this.setLayout(new BorderLayout());
jb=new JButton("Vote");
Dimension dm=jb.getPreferredSize();
rect=new Rectangle(dm.width,1,0,0,Color.BLUE);
rect.setBounds(0,0,dm.width,300);
rect.setRectY(260);
jb.addActionListener(this);
//rect.setRectHeight(100);
add(rect,BorderLayout.NORTH);
add(jb,BorderLayout.SOUTH);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
public void actionPerformed(ActionEvent e)
{
rect.setRectY(rect.getRectY()-2);
rect.setRectHeight(rect.getRectHeight()+2);
}
}class Rectangle extends Canvas
{
private int width,height,x,y;
private Color c;
Rectangle(int width,int height,int x,int y)
{
this.width=width;
this.height=height;
this.x=x;
this.y=y;
c=Color.RED;
}
Rectangle(int width,int height,int x,int y,Color c)
{
this.width=width;
this.height=height;
this.x=x;
this.y=y;
this.c=c;
}
public int getRectY()
{
return this.y;
}
public void setRectY(int y)
{
this.y=y;
}
public void setRectHeight(int height)
{
this.height=height;
repaint();
}
public int getRectHeight()
{
return this.height;
}
public void paint(Graphics g)
{
g.setColor(c);
g.fillRect(x,y,width,height);
}
}帮我看一下这个,用JLabel显示不出来。
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;public class Score extends JFrame implements ActionListener { /**
* @param args
*/
jpanel[] j;
Container contentPane;
int Count;
Score(String title)
{
super(title);
contentPane=this.getContentPane();
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
addPane(6);
initMenu();
}
void addPane(int n)
{
j=new jpanel[n];
Count=n;
for(int i=0;i<n;i++)
{
j[i]=new jpanel();
contentPane.add(j[i]);
}
}
void initMenu()
{
JMenuBar mb=new JMenuBar();
JMenu jmStart=new JMenu("开始");
JMenuItem jmiStart=new JMenuItem("开始");
JMenuItem jmiEnd =new JMenuItem("结束");
JMenuItem jmiExit =new JMenuItem("退出");
jmStart.add(jmiStart);
jmStart.add(jmiEnd);
jmStart.add(jmiExit);
jmiStart.addActionListener(this);
jmiEnd.addActionListener(this);
jmiExit.addActionListener(this);
JMenu jmAdd=new JMenu("增加候选人");
JMenuItem jmiAdd=new JMenuItem("增加");
JMenuItem jmiScore=new JMenuItem("得分");
jmAdd.add(jmiAdd);
jmAdd.add(jmiScore);
jmiAdd.addActionListener(this);
jmiScore.addActionListener(this);
mb.add(jmStart);
mb.add(jmAdd);
setJMenuBar(mb);
}
public void actionPerformed(ActionEvent e)
{
JMenuItem jmi=(JMenuItem)e.getSource();
if(jmi.getActionCommand().equals("退出"))
{
System.exit(0);
}
else if(jmi.getActionCommand().equals("增加"))
{ }
}
public static void main(String[] args) {
// TODO 自动生成方法存根
final Score s=new Score("演讲记分系统");
s.setBounds(100,100,800,400);
//s.setExtendedState(s.MAXIMIZED_BOTH);
s.setDefaultCloseOperation(s.DISPOSE_ON_CLOSE);
s.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
});
s.setVisible(true);
}}class jpanel extends JPanel implements ActionListener
{
JButton jb;
Rectangle rect;
jpanel()
{
this.setBackground(Color.WHITE);
this.setLayout(new BorderLayout());
jb=new JButton("Vote");
Dimension dm=jb.getPreferredSize();
rect=new Rectangle(dm.width,1,0,0,Color.BLUE);
rect.setBounds(0,0,dm.width,300);
rect.setRectY(260);
jb.addActionListener(this);
//rect.setRectHeight(100);
add(Box.createRigidArea(new Dimension(0, 300)), BorderLayout.WEST);
add(rect,BorderLayout.CENTER);
add(jb,BorderLayout.SOUTH);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
public void actionPerformed(ActionEvent e)
{
rect.setRectY(rect.getRectY()-2);
rect.setRectHeight(rect.getRectHeight()+2);
}
}//class Rectangle extends Canvas
class Rectangle extends JLabel
{
//private int width,height,x,y;
private int w,h,x,y;
private Color c;
Rectangle(int width,int height,int x,int y)
{
this.w=width;
this.h=height;
this.x=x;
this.y=y;
c=Color.RED;
}
Rectangle(int width,int height,int x,int y,Color c)
{
this.w=width;
this.h=height;
this.x=x;
this.y=y;
this.c=c;
}
public int getRectY()
{
return this.y;
}
public void setRectY(int y)
{
this.y=y;
}
public void setRectHeight(int height)
{
this.h=height;
repaint();
}
public int getRectHeight()
{
return this.h;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(c);
g.fillRect(x,y,w,h);
}
}
这句起了什么作用? 怎么这么神奇?呵呵
add(Box.createVerticalStrut(300), BorderLayout.WEST);没有text的JLabel默认情况下尺寸为0,所以会被BorderLayout压遍。Box.createVerticalStrut(300)
或者
Box.createRigidArea(new Dimension(0, 300))的作用是建立一个垂直方向的支撑。