java myeclipse在面板上添加的按钮等控件怎么不能显示
import java.awt.*;
class FrameTest extends JFrame
{
public FrameTest()
{
super("面板");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(240,90);
}
}
class PanelTest extends FrameTest
{
JPanel panelobj;
public PanelTest()
{
panelobj=new JPanel();
panelobj.setBackground(Color.white);
getContentPane().add(panelobj);
}
}
class ButtonTest extends PanelTest
{
JButton buttonobj1;
JButton buttonobj2;
public ButtonTest()
{
buttonobj1=new JButton("按钮1");
buttonobj2=new JButton("按钮2");
buttonobj1.setEnabled(true);
buttonobj1.setVisible(true);
panelobj.add(buttonobj1);
panelobj.add(buttonobj2);
}
}
class FlowLayoutTest extends ButtonTest
{
FlowLayout layoutobj;
public FlowLayoutTest()
{
layoutobj=new FlowLayout();
getContentPane().setLayout(layoutobj);
}
}
public class j02_button {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new FlowLayoutTest();
}
} 展开
2016-03-02 · 百度知道合伙人官方认证企业
myeclipse中swing布局的问题导致控件不显示。
例子如下:
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
public class JButtonDemo extends JFrame {
private static final long serialVersionUID = 8651755261675396406L;
private JPanel contentPane;
public static void main(String[] args) {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JButtonDemo frame = new JButtonDemo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public JButtonDemo() {
setTitle("按钮使用");// 设置窗体的标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体退出时操作
setBounds(100, 100, 250, 100);// 设置窗体位置和大小
contentPane = new JPanel();// 创建内容面板
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));// 设置面板的边框
setContentPane(contentPane);// 应用内容面板
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));// 设置内容面板为流式布局
JButton modifyButton = new JButton("修改");// 创建按钮
modifyButton.setIcon(new ImageIcon(JButtonDemo.class
.getResource("/images/modify.gif")));// 为按钮增加图标
contentPane.add(modifyButton);// 在面板上增加按钮
JButton deleteButton = new JButton("删除");// 创建按钮
deleteButton.setIcon(new ImageIcon(JButtonDemo.class
.getResource("/images/delete.gif")));// 为按钮增加图标
contentPane.add(deleteButton);// 在面板上增加按钮
}
}
运行效果:
import javax.swing.*;
import java.awt.*;
public class FrameTest extends JFrame
{
JPanel panelobj;
JButton b1,b2;
public FrameTest()
{
super("面板");
panelobj=new JPanel();
b1=new JButton("1");b2=new JButton("2");
panelobj.add(b1);
panelobj.add(b2);
panelobj.setBackground(Color.white);
add(panelobj);
getContentPane().setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(240,90);
}
public static void main(String[] args) {
new FrameTest();
}
}
吧按钮隐藏掉了! 要把它设置为false!;
我安你说的试了哈 还是没有显示呀
是这里buttonobj1.setEnabled(true);
buttonobj1.setVisible(true);
改成false试试看!