jpanel

Java Swing - JLabel Location

老子叫甜甜 提交于 2019-12-10 18:13:13
问题 I have problem while setting the Jlabel location. I set the content pane to some JPanel, I created and tried to add my JLabel. JLabel mainTitle = new JLabel("SomeApp"); mainTitle.setFont(new Font("Arial",2 , 28)); mainTitle.setBounds(0,0, 115, 130); getContentPane().add(mainTitle); I want that my JPanel will be on the top left corner on my application and what I am getting is "SomeApp" on the top center.(and not top left). btw I tried to add JButton the and the I can`t change the width,height

FlowLayout not flowing with JScrollPane around it

浪尽此生 提交于 2019-12-10 17:56:40
问题 I have a bunch of buttons on a JPanel using a FlowLayout. It looks really nice. When the buttons reach the right side of the panel they start out on a new row creating a nice 2-dimensional grid. Here is the code: Container cp = getContentPane(); JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); for (int i = 0; i < 20; i++) panel.add(new JButton("Button " + i)); cp.add(panel); However, the minute I put the panel in a scroll pane with only vertical scrolling: Container cp =

dynamically change color of custom graphic

拜拜、爱过 提交于 2019-12-10 17:29:03
问题 Problem: graphics aren't repainted until after methods are run. When button clicked two methods are called. Inside each method is code that is supposed to change the color of the graphic associated with this method (in the UI); when the method starts the graphic is changed from black to green; when the method finishes the color is changed from green to red. Then the next method is called and its graphic should turn green (method is running) and when the method finishes its graphic should be

Switching between screens in Java swing

妖精的绣舞 提交于 2019-12-10 17:25:25
问题 I had build big application for my client in Android. The new project is to build the same app for PC in Java. So i have started developing with use of swing. When building in Android i use classes with activities that had content views and i could switch between activities. In java i have build multiple classes that extends JFrame and i wish to switch between them. What is the right way to to this? is this: new_class.setVisible(true); old_class.setVisible(false); the correct way? 回答1: The

Adding buttons using gridlayout

孤者浪人 提交于 2019-12-10 17:06:40
问题 I'm trying to create a simple tic tac toe board made by 9x9 JButtons. I used a 2d array and a gridlayout but the result is nothing, a frame without any button. What I'm doing wrong? import java.awt.GridLayout; import javax.swing.*; public class Main extends JFrame { private JPanel panel; private JButton[][]buttons; private final int SIZE = 9; private GridLayout experimentLayout; public Main() { super("Tic Tac Toe"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500,500);

Panel components disappear when I minimize frame

主宰稳场 提交于 2019-12-10 17:04:55
问题 I have a layout with a main panel whose content is decided by me throught buttons in this way: public void actionPerformed(ActionEvent e) { mainPanel.removeAll(); //removing all current panel components if(e.getActionCommand().equals("content1")){ mainPanel = new Content1Panel(); add(mainPanel,BorderLayout.CENTER); validate(); }else if(e.getActionCommand().equals("content2")){ mainPanel = new Content2Panel(); add(mainPanel,BorderLayout.CENTER); validate(); } } now, if I minimize the frame and

What is a good way to Organize multiple JPanel's in a JFrame?

痞子三分冷 提交于 2019-12-10 15:55:21
问题 What I am trying to do is organize five seperate JPanel's within the frame. Here is what the output is supposed to look like: There will be one panel across the top. Two panels directly below the top panel that vertically split the space and then another two panels that split the remaining space horizontally. I cannot figure out how to organize the panels like described above and I think it is because I just don't know the proper syntax. So any help or advise is greatly appreciated here is

Passing font to components on a JPanel

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 15:19:34
问题 I have a class that extends JPanel with several buttons on it. I would like to be able to set the font on all the buttons with one call to setFont(Font font); I defined the setFont method in the JPanel class as such: public class MyPanel extends JPanel { private JButton[] buttons = new JButton[10]; public MyPanel() { for(int i = 0; i < 10; i++) { buttons[i] = new JButton(""+i); this.add(buttons[i]); } } public void setFont(Font font) { if(buttons != null) { for(JButton b : buttons) { b

Remove key listener from panel

梦想的初衷 提交于 2019-12-10 14:49:40
问题 Is it possible to clear a listener that I put on my JPanel ? When I call a method, I put a KeyListener on the panel but when I quit this method, I want to clear that listener. Here is my method : private void stopBall(final Graphics2D g2, int posBallY, String winner) { move = false; scorePanel.showPressSpace(true); setFocusable(true); requestFocus(); addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e){ if (e.getKeyCode() == KeyEvent.VK_SPACE){ setPosX(getPlayPanelWidth()/2);

setPreferredSize does not work

南笙酒味 提交于 2019-12-10 14:30:11
问题 Code: import java.awt.Dimension; import javax.swing.*; public class Game extends JFrame { private static final long serialVersionUID = -7919358146481096788L; JPanel a = new JPanel(); public static void main(String[] args) { new Game(); } private Game() { setTitle("Insert name of game here"); setLocationRelativeTo(null); setLayout(null); setDefaultCloseOperation(EXIT_ON_CLOSE); a.setPreferredSize(new Dimension(600, 600)); add(a); pack(); setVisible(true); } } So I set the preferred size of the