jpanel

Adding background image to JPanel on button action

对着背影说爱祢 提交于 2019-12-17 20:42:26
问题 What is the best way to add a background image to a JPanel/JLabel when a JButton is called? I know how to get the JButton action and such. I just can't figure out or find a way to get the background image to change when that button is pressed. 回答1: Here is an example: import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.URL;

Java swing; How to toggle panel's visibility?

大憨熊 提交于 2019-12-17 19:32:32
问题 i made this code to navigate trough panel1 and panel2 with buttons. (button1 and button2) but when i run my code the frame stays empty. Can somebody explain me what i'm doing wrong and how i can accomplish toggling between panel1 and panel2 in this way? Starting with panel1 first Code: import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JFrame; public class togglepanel { public static void main

Java :Add scroll into text area

南楼画角 提交于 2019-12-17 18:49:04
问题 How can i add the scroll bar to my text area. i have tried with this code but it's not working. middlePanel=new JPanel(); middlePanel.setBorder(new TitledBorder(new EtchedBorder(), "Display Area")); // create the middle panel components display = new JTextArea(16, 58); display.setEditable(false); // set textArea non-editable scroll = new JScrollPane(display); scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); //Add Textarea in to middle panel middlePanel.add

Update graph with JFreeChart and slider

房东的猫 提交于 2019-12-17 16:49:09
问题 I have a time history for arrays describing the pressure along a pipe. So I have an array of pressure values along the length of the pipe for each delta t. I want to plot the pressures along the length of the pipe with JFreeChart and chose which delta t to plot with a slider, so that whenever the user moves the slider the graphic is updates with values from a different delta t. I'm also resetting the tile to be the pressure at the last portion of the pipe. What happens is that the title is

Java JPanel inside JScrollPane?

有些话、适合烂在心里 提交于 2019-12-17 16:32:58
问题 I have a JFrame, in this JFrame I have a JPanel that I draw on, this Panel can be any size and so I placed it into a JScrollpane to let me scroll when the panel is larger than the window screen size. Unfortunately does not work as I expected: Making the JFrame window smaller than the JPanel size does not show scroll bars The JScrollPane size now seems locked to the size of the JPanel I have added to it, where as before it resized to the bounds of it's JFrame window (it still kinda does this

How to set a transparent background of JPanel?

核能气质少年 提交于 2019-12-17 15:39:42
问题 Can JPanel s background be set to transparent? My frame is has two JPanel s: Image Panel and Feature Panel . Feature Panel is overlapping Image Panel . The Image Panel is working as a background and it loads image from a remote URL. On Feature Panel I want to draw shapes. Now Image Panel cannot be seen due to Feature Panel's background color. I need to make Feature Panel background transparent while still drawing its shapes and I want Image Panel to be visible (since it is doing tiling and

Not showing graphics in JPanel which is added to another JPanel

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 14:54:19
问题 When adding a JPanel that has graphics to a JFrame , it's working fine. But when I try to add a JPanel in which I have added another JPanel with graphics, its not showing in the JFrame . Please see code below package sample; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Main extends JFrame{ public static void main(String[] args) { new Main(); } public Main(){ setTitle("Sample"); setVisible(true); setSize(500,500);

Change size of JPanel using CardLayout

女生的网名这么多〃 提交于 2019-12-17 14:15:23
问题 is it possible to change the size of Jpanels when using Java CardLayout? 回答1: shoot, something like this where the component (here a JLabel rather than a JPanel) has it's preferredSize set, then place it in another JPanel that uses an appropriate layout, here GridBagLayout which with default settings will center the component, and add the GridBagLayout using JPanel to the CardLayout using panel:: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border

Convert JPanel to image

感情迁移 提交于 2019-12-17 12:44:50
问题 Is there a way to convert a JPanel (that has not yet been displayed) to a BufferedImage? thanks, Jeff 回答1: From the BufferedImage you can create a graphics object, which you can use to call paint on the JPanel, something like: public BufferedImage createImage(JPanel panel) { int w = panel.getWidth(); int h = panel.getHeight(); BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); panel.paint(g); g.dispose(); return bi; } You may need to

Convert JPanel to image

妖精的绣舞 提交于 2019-12-17 12:44:21
问题 Is there a way to convert a JPanel (that has not yet been displayed) to a BufferedImage? thanks, Jeff 回答1: From the BufferedImage you can create a graphics object, which you can use to call paint on the JPanel, something like: public BufferedImage createImage(JPanel panel) { int w = panel.getWidth(); int h = panel.getHeight(); BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); panel.paint(g); g.dispose(); return bi; } You may need to