jpanel

Switch between JPanels of different classes

*爱你&永不变心* 提交于 2020-01-05 09:33:58
问题 How do I switch between JPanels of different classes? I do not wish to use a Card Layout. I have 2 classes - MainPage & MenuPage . For instance, I would like to clear the contentPane (a JPanel ) @ MainPage and replace it with the content pane @ MenuPage . For testing purposes, I included a button @ MenuPage . Please see my following attempt - it gives an error: java.lang.IllegalArgumentException: adding a window to a container MainPage public class MainPage extends JFrame { private static

Design Issue: MVC model with Java CardLayout application

你说的曾经没有我的故事 提交于 2020-01-05 08:34:05
问题 Sorry for the vague title, I am trying to layout an application i have unfortunately starting coding without following proper MVC architecture rules. I have an application that has the following structure. JFrame with a JPanel ("MainPanel") added as a component and the MainPanel has a CardLayout structure. The MainPanel then has several JPanel's that are flipped through using buttons, Jcombo Box and List selections. I want to implement the MVC model using this structure and i am having

How can I move the image in GUI

心已入冬 提交于 2020-01-05 06:02:50
问题 How can I set the image in this GUI to the far right, if you run the code you will see that the image goes under the radio buttons and checkboxes, I want to move the image to the right so It doesn't do that. How can I do that? import javax.swing.*; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import

A simple way to setting a bufferedImage into a single colored pixel without placing a image into it?

孤人 提交于 2020-01-05 04:43:24
问题 So I just want to set a buffered image into a single background color is there a way to do this? 回答1: Do you mean fill a BufferedImage with a background color? If so, here is an example on how to perform this: import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class TestBufferedImage { private BufferedImage buffer; protected

Increasing distance from top of JPanel

爱⌒轻易说出口 提交于 2020-01-04 14:23:28
问题 I'm trying to increase the distance of my JButtons from the top of my Panel to make it more visually appealing, i've tried using an invisible button but have had no luck. public class SimpleBorder { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(500,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Border etched = (Border) BorderFactory.createEtchedBorder(); String[] items = {"A", "B", "C", "D"}; JList list = new JList(items); JTextArea text =

Image Drawing Works for JFrame, But Not JPanel

北城余情 提交于 2020-01-04 07:54:09
问题 I'm working through some simple applications to get familiar with Swing and running into problems. I'm attempting to have a frame containing an image (in a panel) along with buttons to zoom in/out from the image. I have been able to make a frame with the added image work fine (albeit with some frame sizing issues, but that is another story), however, when I call the same component class to add it to a panel, nothing appears. I'm hoping one of you can help shed light on the situation. CODE:

JButtons inside JPanels, fill up the whole panel

痞子三分冷 提交于 2020-01-04 06:31:30
问题 I've been struggling to set a specific size to a button inserted into a JPanel with a GridLayout. The button always fills up the whole panel, whereas if I remove the gridlayout, the button won't have the same behavior. any hints? package panels; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing

JPanel and JButton, cannot figure how to layout 2 simple buttons

和自甴很熟 提交于 2020-01-04 05:26:35
问题 i am starting with JPanel and i am trying to put 2 simple buttons on a frame, i was able to put the buttons but not position them, here is my code: JFrame frame = new JFrame(); frame.setSize(500,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton but = new JButton("text"); JButton but2 = new JButton("list"); JPanel panel= new JPanel(new GridLayout(1, 2)); panel.setSize(100, 100); panel.add(but); panel.add(but2); frame.add(panel); frame.setVisible(true); and here is a sketch of

JTabbedPane why is there extra padding only when I have multiple tabs? (code and picture)

主宰稳场 提交于 2020-01-04 04:12:06
问题 I have a JTabbed pane, which has a varying number of tabs. When the number of tabs is greater than 4, I get extra spacing/padding at the bottom of each tab panel. The picture below shows this (on the left you see the extra spacing, on the right you see no extra spacing). Here is the exact code I used to get those pictures: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane;

java swing associating JPanel with row col values for mouse listener

独自空忆成欢 提交于 2020-01-04 04:11:09
问题 I'm writing a board game with a GUI and basically I have a 10x10 GridLayout JPanel. Each grid cell is a square JPanel (I used BorderLayout for these JPanels so the borders are visible). Anyway I want it so that when one of these squares is clicked, it makes a change to the boardGameGrid which is a class imported to the GUI for the backend of the game. Say I want to use the method boardGameGrid.setCellCross(x,y) when the cell at position (x,y) is pressed. I can't figure out how to do this