jpanel

JButton in JPanel with gridlayout

*爱你&永不变心* 提交于 2021-02-08 11:34:36
问题 Is it possible to bring it into this shape using the gridlayout? I use empty label to add this empty grid This is my code: import javax.swing.*; import java.awt.*; public class test { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("Buttons"); frame.setSize(800, 800); int size=12; int flag1=0,count=0,count2=0; frame.setLayout(new GridLayout(1,2,0,0)); JPanel panel = new JPanel(); GridLayout layout = new GridLayout(size,(size*2),0,0); panel.setLayout(layout

Java Swing - How to disable a JPanel?

时间秒杀一切 提交于 2021-02-07 11:17:45
问题 I have several JComponent s on a JPanel and I want to disable all of those components when I press a Start button. At present, I am disabling all of the components explicitly by component1.setEnabled(false); : : But Is there anyway by which I can disable all of the components at once? I tried to disable the JPanel to which these components are added by panel.setEnabled(false); but it didn't work. 回答1: The panel should have a getComponents() method which can use in a loop to disable the sub

How can I add some sound to my Java JFrame? [closed]

旧巷老猫 提交于 2021-02-05 12:20:05
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 months ago . Improve this question I am making a Java game in JFrame. The game is almost completed but I want to add some sound to it. Like when the game will start, the sound should also start. I have checked the Internet but the codes are either not working or are very long. Can anyone help

Java Graphics not showing on Mac even with overriding the paintComponent() method of the JPanel

和自甴很熟 提交于 2021-02-05 11:21:05
问题 I'm working on a small game project and am having an issue with java graphics not being displayed. I've searched similar questions here on Stackoverflow and used overriding the paintComponent() method of the JPanel which many ppl advised but am still having this issue. I hope to get my background image and menubar, and music at the same time. Music works fine but background image and menubar doesn't show up at all. here's my code : import java.awt.Graphics; import java.awt.Image; import javax

Add Jbutton to Jpanel

给你一囗甜甜゛ 提交于 2021-02-05 11:13:30
问题 can somebody tell me what is wrong with this code i am trying to add the buttons to my JPanel ArrayList<JButton> buttons = new ArrayList<JButton>(); JPanel createButtonspane(){ bpanel = new JPanel(); for(int i=0; i<10; i++){ buttons.add(new JButton(""+i)); bpanel.add(buttons); } return bpanel; } 回答1: This code does not compile because JPanel does not have an overload of add() which takes an array of JButton s, so you can not add a whole array of buttons to the JPanel (even if it was possible,

Add Jbutton to Jpanel

可紊 提交于 2021-02-05 11:10:16
问题 can somebody tell me what is wrong with this code i am trying to add the buttons to my JPanel ArrayList<JButton> buttons = new ArrayList<JButton>(); JPanel createButtonspane(){ bpanel = new JPanel(); for(int i=0; i<10; i++){ buttons.add(new JButton(""+i)); bpanel.add(buttons); } return bpanel; } 回答1: This code does not compile because JPanel does not have an overload of add() which takes an array of JButton s, so you can not add a whole array of buttons to the JPanel (even if it was possible,

Revalidate JPanel's parent frame

安稳与你 提交于 2021-02-05 07:09:25
问题 I want my panel to fit the frame when I show/hide fields. How could I notify to the parent frame to revalidate? I thought about pass the frame to my panel's constructor, but I think may have a way this is already done. I remember that there was a protected attribute in JPanel, but there isn't.. maybe i remembering the wrong component. 回答1: I need show/hide some fields of my panel, and I want the panel to fit the frame. You mean like a summary/details view and you want the frame size to change

Not all of my JButtons are showing up after I add a dozen of them. What am I doing wrong?

烂漫一生 提交于 2021-01-29 22:43:24
问题 I want to add 52 buttons in a JPanel. All with ActionListeners. When I reach a certain amount (13) and run the program, not all of the buttons show up. For example I've added 15 buttons and only 9 or 12 of them show up. Sometimes all of them, but not every time. Here's the code for two of the JButtons: JButton button_one=new JButton(); button_one.setPreferredSize(new Dimension(100,150)); mainpanel.add(button_one); button_one.addActionListener(new ActionListener(){ @Override public void

Not all of my JButtons are showing up after I add a dozen of them. What am I doing wrong?

北城余情 提交于 2021-01-29 22:40:49
问题 I want to add 52 buttons in a JPanel. All with ActionListeners. When I reach a certain amount (13) and run the program, not all of the buttons show up. For example I've added 15 buttons and only 9 or 12 of them show up. Sometimes all of them, but not every time. Here's the code for two of the JButtons: JButton button_one=new JButton(); button_one.setPreferredSize(new Dimension(100,150)); mainpanel.add(button_one); button_one.addActionListener(new ActionListener(){ @Override public void

Not all of my JButtons are showing up after I add a dozen of them. What am I doing wrong?

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-29 22:02:13
问题 I want to add 52 buttons in a JPanel. All with ActionListeners. When I reach a certain amount (13) and run the program, not all of the buttons show up. For example I've added 15 buttons and only 9 or 12 of them show up. Sometimes all of them, but not every time. Here's the code for two of the JButtons: JButton button_one=new JButton(); button_one.setPreferredSize(new Dimension(100,150)); mainpanel.add(button_one); button_one.addActionListener(new ActionListener(){ @Override public void