Java 如何将按钮设置为可见?
我的问题是,即使我使用setVisible(true)将按钮可见属性设置为“true”,但按钮仍然不可见。当我检查按钮isVisible时,它显示为true,但按钮不在那里 我添加了让系统在非常重要的一行代码和setVisible之后打印出来的消息Java 如何将按钮设置为可见?,java,swing,awt,jbutton,visible,Java,Swing,Awt,Jbutton,Visible,我的问题是,即使我使用setVisible(true)将按钮可见属性设置为“true”,但按钮仍然不可见。当我检查按钮isVisible时,它显示为true,但按钮不在那里 我添加了让系统在非常重要的一行代码和setVisible之后打印出来的消息 public void buttonarrays(){ inGame = new JButton(); inGame.setVisible(true); inventory = new JButton()
public void buttonarrays(){
inGame = new JButton();
inGame.setVisible(true);
inventory = new JButton();
inventory.setVisible(false);
add(inGame);
add(inventory);
inGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(true){
switch(0) {
case 0:
inventory.setVisible(true);
inGame.setVisible(false);
break;
}
}
}
});
}
我试图最小化它,希望你仍然理解我的意思这是按钮的工作代码 请尝试它,学习它,然后尝试找出您在代码中遇到的问题
import javax.swing.JButton;
import javax.swing.JFrame;
public class JButtonExample {
JButtonExample(){
JFrame frame=new JFrame();
// Creating Button
JButton b=new JButton("Click Me..");
b.setBounds(50,50,90, 50);
//Adding button onto the frame
frame.add(b);
// Setting Frame size. This is the window size
frame.setSize(300,200);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonExample();
}
}
嗯,您的按钮既没有文本也没有图标,因此根据容器的布局管理器,它可能会非常小。说到布局管理器:你们班用哪一个?该方法中的组件是什么类型的(
add(inGame)
表示您的类扩展了一些容器组件)?您是否将容器添加到框架/窗口(可能使用中间容器)?它们使用什么布局?提示:你不能从实验中学习swing/awt。这些框架需要一定程度的知识才能为您做一些有用的事情。只要想一些基于“嗯,这可能会起作用”的代码,请放心:它不会起作用。取而代之的是:找一本好书,或者简单地阅读oracle教程()并阅读/使用工作示例代码。你在这里做什么。。。诚实地说,总结一些想法:你在浪费你的时间和精力。它有一个图标,我刚把它拿出来,所以它的代码更少