为什么JFrame中只能显示一个控件? 因为你使用的是BorderLayout,在BorderLayout被使用的情况下,调用rootFrame.getContentPane().add时,如果没有指定安放的位置,则会默认把空间放在中央并伸展至四周,所以你可以指定排放的位置,如NORTH, SOUTH, CENTER, EAST, WEST等,也可以改用别的Layout,如FlowLayout。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class SwingSample implements ActionListener{ private JFrame rootFrame=null; private JButton exitButton=null; private JButton clickMeButton=null; private JLabel rootLabel=null; private JLabel clickNumsLabel=null; private int m_iClickNum=0; private String m_strLabelPrefix="一共点击了:"; private String m_strLabelPostfix="次"; private Hashtable componentTable; public SwingSample() { componentTable=new Hashtable(10); rootFrame=new JFrame(); rootLabel=new JLabel(); rootFrame.getContentPane().setLayout(new FlowLayout());///////////////在这里 rootFrame.setDefaultLookAndFeelDecorated(true); //添加“退出”按纽 exitButton=new JButton("退出"); exitButton.setMnemonic(KeyEvent.VK_Q); exitButton.addActionListener(this); // clickMeButton =new JButton("点击"); clickMeButton.setMnemonic(KeyEvent.VK_C); clickMeButton.addActionListener(this); // clickNumsLabel=new JLabel(m_strLabelPrefix +m_iClickNum+m_strLabelPostfix); // rootFrame.getContentPane().add(exitButton); rootFrame.getContentPane().add(clickMeButton); rootFrame.getContentPane().add(clickNumsLabel); rootFrame.pack(); rootFrame.setVisible(true); componentTable.put("RootFrame",rootFrame); componentTable.put("ExitButton",exitButton); } // // // private JFrame getRootFrame() { return rootFrame; } // public static void main(String[] args) { SwingSample theApp=new SwingSample(); } // public void actionPerformed(ActionEvent e) { if(e.getSource()==exitButton) { System.exit(0); } else if(e.getSource()==clickMeButton) { m_iClickNum+=1; clickNumsLabel.setText(m_strLabelPrefix+m_iClickNum+m_strLabelPostfix); } }} 怎么设置调用本窗口类的窗口的状态 short s1 = 1; s1 = s1 + 1;问题 求教:如何提高计算精确度? menu效果 java如何生成高质量缩略图 字符串与二进制之间的转换 急求正则表达式:不以某些字符开头,需要以某些字符结束 请问大家都用java来做什么?(学子的疑惑) 一个文件搜索小工具,实用,有源码 如何改变java应用程序标题栏的那个茶碗? 1.一个对象可不可以改变 2.System.out.println( )的print 基本数据类型(int)问题 请高手从JVM内存分配的角度来解释 如何提高读取文件,字符串替换,再生成新文件的执行效率??我给100分
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class SwingSample implements ActionListener
{
private JFrame rootFrame=null;
private JButton exitButton=null;
private JButton clickMeButton=null;
private JLabel rootLabel=null;
private JLabel clickNumsLabel=null;
private int m_iClickNum=0;
private String m_strLabelPrefix="一共点击了:";
private String m_strLabelPostfix="次";
private Hashtable componentTable;
public SwingSample()
{
componentTable=new Hashtable(10);
rootFrame=new JFrame();
rootLabel=new JLabel();
rootFrame.getContentPane().setLayout(new FlowLayout());///////////////在这里
rootFrame.setDefaultLookAndFeelDecorated(true);
//添加“退出”按纽
exitButton=new JButton("退出");
exitButton.setMnemonic(KeyEvent.VK_Q);
exitButton.addActionListener(this);
//
clickMeButton =new JButton("点击");
clickMeButton.setMnemonic(KeyEvent.VK_C);
clickMeButton.addActionListener(this);
//
clickNumsLabel=new JLabel(m_strLabelPrefix +m_iClickNum+m_strLabelPostfix); // rootFrame.getContentPane().add(exitButton);
rootFrame.getContentPane().add(clickMeButton);
rootFrame.getContentPane().add(clickNumsLabel);
rootFrame.pack();
rootFrame.setVisible(true); componentTable.put("RootFrame",rootFrame);
componentTable.put("ExitButton",exitButton);
}
//
//
//
private JFrame getRootFrame()
{
return rootFrame;
} //
public static void main(String[] args)
{
SwingSample theApp=new SwingSample();
}
//
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==exitButton)
{
System.exit(0);
}
else
if(e.getSource()==clickMeButton)
{
m_iClickNum+=1;
clickNumsLabel.setText(m_strLabelPrefix+m_iClickNum+m_strLabelPostfix);
}
}
}