It says the constructor JPanel(ImageIcon) is undefined.
Here is partial of my code
public Method_1(Test aa) {
this.aa = aa;
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 730, 540);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//JPanel panel = new JPanel();
BufferedImage myPicture = ImageIO.read(new File("method_1.png"));
JPanel picLabel = new JPanel(new ImageIcon(myPicture));
add(picLabel);
picLabel.setBounds(12, 34, 369, 175);
Change:
JPanel picLabel = new JPanel(new ImageIcon(myPicture));
To:
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
Related
I want to add a JButton (panButton) in the lower area of this GUI, but I don’t know to which element I have to add it. I tried to do panButton.add(element) with several elements but nothing works.
I can't understand what is the main element of the panel.
This is how the GUI appears:
While this is the code:
private void buildGUI() {
setTitle("Avanzamento upload");
setIconImage(Toolkit.getDefaultToolkit().getImage(ClientUpload.class.getResource("/clientupload/resources/logoRFI.gif")));
addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
JLabel lblProgressAll = new JLabel("Totale: ");
JLabel lblProgressCurrent = new JLabel("File attuale: ");
JLabel lblTotalBytes = new JLabel("MB totali: ");
JLabel lblCopiedBytes = new JLabel("MB copiati: ");
lblTotalBytesValue = new JLabel("0 MB");
lblCopiedBytesValue = new JLabel("0 MB");
progressButton = new JButton("Interrompi"); //fc
progressAll = new JProgressBar(0, 100);
progressAll.setStringPainted(true);
progressCurrent = new JProgressBar(0, 100);
progressCurrent.setStringPainted(true);
txtDetails = new JTextArea(5, 50);
txtDetails.setEditable(false);
DefaultCaret caret = (DefaultCaret) txtDetails.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
JScrollPane scrollPane = new JScrollPane(txtDetails, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel contentPane = (JPanel) getContentPane();
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panProgressLabels = new JPanel(new BorderLayout(0, 5));
JPanel panProgressBars = new JPanel(new BorderLayout(0, 5));
JPanel panCopyLabels = new JPanel(new BorderLayout(0, 5));
JPanel panCopyFields = new JPanel(new BorderLayout(0, 5));
panProgressLabels.add(lblProgressAll, BorderLayout.NORTH);
panProgressLabels.add(lblProgressCurrent, BorderLayout.CENTER);
panCopyLabels.add(lblTotalBytes, BorderLayout.NORTH);
panCopyLabels.add(lblCopiedBytes, BorderLayout.CENTER);
panCopyFields.add(lblTotalBytesValue, BorderLayout.NORTH);
panCopyFields.add(lblCopiedBytesValue, BorderLayout.CENTER);
panProgressBars.add(progressAll, BorderLayout.NORTH);
panProgressBars.add(progressCurrent, BorderLayout.CENTER);
JPanel panProgress = new JPanel(new BorderLayout(0, 5));
panProgress.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Avanzamento"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
JPanel panCopy = new JPanel(new BorderLayout(0, 5));
panCopy.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Stato"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
JPanel panDetails = new JPanel(new BorderLayout());
panDetails.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Dettagli"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
//fc
JPanel panButton = new JPanel(new BorderLayout());
panButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(" "), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
panProgress.add(panProgressLabels, BorderLayout.LINE_START);
panProgress.add(panProgressBars, BorderLayout.CENTER);
panCopy.add(panCopyLabels, BorderLayout.LINE_START);
panCopy.add(panCopyFields, BorderLayout.CENTER);
panDetails.add(scrollPane, BorderLayout.CENTER);
JPanel panUpper = new JPanel(new BorderLayout());
panUpper.add(panProgress, BorderLayout.NORTH);
panUpper.add(panCopy, BorderLayout.SOUTH);
contentPane.add(panUpper, BorderLayout.NORTH);
contentPane.add(panDetails, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
}
I’d like it appears in the Stato area on the right, or under the Dettagli area.
Can anyone help me?
panButton is a JPanel which is never added to the GUI so if you add something to it, it -also- wont be added to the visible GUI.
You need to add the button to the button panel and add the button panel to the contentPane
JPanel panButton = new JPanel(new BorderLayout());
panButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(" "), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
panButton.add(element); //Add button to panel
...
contentPane.add(panUpper, BorderLayout.NORTH);
contentPane.add(panDetails, BorderLayout.CENTER);
contentPane.add(panButton, BorderLayout.CENTER); //Add panel to contentPane (which I assume is added to the frame at some point as I can see it's elements on the UI you posted)
or just add it the 'Stato' area with panCopy.add(element);
Finally I created a new JPanel and I added the button there.
I am trying to open up an JFrame with an gif-loading animation from another JFrame. The problem is, that the new JFrame opens but the gif animations just shows up if the loading time is over, so there is a empty JFrame during the whole loading sequence.
I am trying to open up my new JFrame in my current JFrame like this:
Ladescreen loading = new Ladescreen();
loading.setVisible(true);
loading.pack();
And this is how my Loadingscreen is built:
public Ladescreen() {
setTitle("Crawling through files...");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 157, 207);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel picture = new JLabel();
picture.setIcon(new ImageIcon("football.gif"));
panel.add(picture);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel = new JLabel("Loading...");
panel_1.add(lblNewLabel);
}
Can you tell me how to fix this up ?
Here is my code:
final JTextArea textArea = new JTextArea();
textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13));
textArea.setLineWrap(true);
textArea.setBounds(77, 310, 474, 136);
//contentPane.add(textArea); (edited...still the same problem persists..)
JScrollPane sbrText = new JScrollPane(textArea);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
contentPane.add(sbrText);
When ever I try with this,the text area is not visible..(I am using Eclipse's Window Builder plugin and layout as "Absolute Layout")..
Here I have done, the same thingy using Nested Layout, have a look at the code example :
import java.awt.*;
import javax.swing.*;
public class WelcomeExample
{
private JPanel headerPanel;
private JButton logoutButton;
private JPanel leavePanel;
private JRadioButton casualRButton;
private JRadioButton specialRButton;
private JRadioButton sickRButton;
private JRadioButton privilegeRButton;
private ButtonGroup radioButtonGroup;
private JTextField leaveDaysField;
private JButton checkLeaveButton;
private JTextArea notesArea;
private JScrollPane notesScroller;
private JButton applyLeaveButton;
private String headerText = "<html><body><h1><font " +
"color=\"red\">Welcome : </font><font color" +
"=\"blue\">Code Zero</font></h1></body></html>";
private void displayGUI()
{
JFrame frame = new JFrame("Welcome");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
headerPanel = getHeaderPanel();
leavePanel = getLeavePanel();
contentPane.add(headerPanel, BorderLayout.PAGE_START);
contentPane.add(leavePanel, BorderLayout.CENTER);
contentPane.add(getApplyPanel(), BorderLayout.PAGE_END);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel getHeaderPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JLabel headerLabel = new JLabel(headerText, JLabel.CENTER);
JPanel buttonPanel = new JPanel();
logoutButton = new JButton("Logout");
buttonPanel.add(logoutButton);
panel.add(headerLabel, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.LINE_END);
panel.add(new JSeparator(
SwingConstants.HORIZONTAL), BorderLayout.PAGE_END);
return panel;
}
private JPanel getLeavePanel()
{
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel leaveHeaderPanel = new JPanel();
leaveHeaderPanel.setLayout(new GridLayout(0, 1, 5, 5));
leaveHeaderPanel.setBorder(
BorderFactory.createTitledBorder("Choose a leave type : "));
JPanel leaveTypePanel = new JPanel();
leaveTypePanel.setLayout(new FlowLayout(
FlowLayout.LEFT, 5, 5));
casualRButton = new JRadioButton("Casual Leave");
specialRButton = new JRadioButton("Special Leave");
sickRButton = new JRadioButton("Sick Leave");
privilegeRButton = new JRadioButton("Privilege Leave");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(casualRButton);
radioButtonGroup.add(specialRButton);
radioButtonGroup.add(sickRButton);
radioButtonGroup.add(privilegeRButton);
leaveTypePanel.add(casualRButton);
leaveTypePanel.add(specialRButton);
leaveTypePanel.add(sickRButton);
leaveTypePanel.add(privilegeRButton);
JPanel applyLeavePanel = new JPanel();
applyLeavePanel.setLayout(new FlowLayout(
FlowLayout.LEFT, 5, 5));
JLabel applyLeaveLabel = new JLabel(
"Apply for (No. of days) : ", JLabel.CENTER);
leaveDaysField = new JTextField(5);
checkLeaveButton = new JButton("Check Leave Availability");
applyLeavePanel.add(applyLeaveLabel);
applyLeavePanel.add(leaveDaysField);
applyLeavePanel.add(checkLeaveButton);
leaveHeaderPanel.add(leaveTypePanel);
leaveHeaderPanel.add(applyLeavePanel);
notesArea = new JTextArea(10, 10);
notesScroller = new JScrollPane();
notesScroller.setBorder(
BorderFactory.createTitledBorder(
"Leave Note (Max. 200 Characters) : "));
notesScroller.setViewportView(notesArea);
panel.add(leaveHeaderPanel, BorderLayout.PAGE_START);
panel.add(notesScroller, BorderLayout.CENTER);
return panel;
}
private JPanel getApplyPanel()
{
JPanel panel = new JPanel();
applyLeaveButton = new JButton("Apply");
panel.add(applyLeaveButton);
return panel;
}
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
#Override
public void run()
{
new WelcomeExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
OUTPUT :
I don't think you need to do contentPane.add(textArea);. It is this line that is causing the problem. Comment out this and your code should work fine.
See this answer, it might help you.
The following code runs fine at my place :
final JTextArea textArea = new JTextArea();
textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13));
textArea.setLineWrap(true);
textArea.setBounds(77, 310, 474, 136);
JScrollPane sbrText = new JScrollPane(textArea);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(sbrText);//contentPane.add(sbrText);
frame.setVisible(true);
If your code is not running fine then you must have some other error probably related to your contentpane.
Ok...I finally got it to work...I specified the same set bounds in the scroll pane instead of the text area.. Here is the code.!!! ^_^
final JTextArea textArea = new JTextArea();
textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13));
textArea.setLineWrap(true);
//textArea.setBounds(77, 310, 474, 136);
JScrollPane scroll = new JScrollPane (textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setBounds(77, 310, 474, 136);
contentPane.add(scroll);
Here are the screenshots:
Previous: http://oi40.tinypic.com/11jyum0.jpg
Now: http://oi44.tinypic.com/2s9vdvt.jpg
So, I'm starting with the basic visual Java components, and have trouble to use them accurately.
Here is my new problem: I'm trying to implement a 3 parts panel: east, center, west, but can't have a proper display of the center panel. Here is my piece of code:
Basically 'panelUpMiddle' isn't visible, so I wonder why?...
public class TestCode_Web {
public static void main(String[] args) {
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 400);
JPanel innerPanel = new JPanel(new BorderLayout());
JPanel panelUp = new JPanel(new BorderLayout());
JPanel panelUpLeft = new JPanel();
JPanel panelUpMiddle = new JPanel();
window.add(innerPanel, BorderLayout.NORTH);
innerPanel.add(panelUp, BorderLayout.NORTH);
panelUp.add(panelUpLeft, BorderLayout.WEST);
panelUp.add(panelUpMiddle, BorderLayout.CENTER);
JLabel label1 = new JLabel("Label 1");
JLabel label11 = new JLabel("Label 11");
JLabel label12 = new JLabel("Label 12");
panelUp.add(label1);
panelUpLeft.add(label11);
panelUpMiddle.add(label12);
panelUp.setBackground(new Color(200, 240, 200));
panelUpLeft.setBackground(new Color(200, 240, 0));
panelUpMiddle.setBackground(new Color(100, 240, 200));
panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));
panelUpLeft.setPreferredSize(new Dimension(160, 120));
window.setVisible(true);
}
}
Try this code. I updated the naming of the panels and the labels so it is more clear.
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 400);
JPanel centerPanel = new JPanel(new BorderLayout());
JPanel eastPanel = new JPanel(new BorderLayout());
JPanel westPanel = new JPanel(new BorderLayout());
window.add(centerPanel, BorderLayout.CENTER);
window.add(eastPanel, BorderLayout.EAST);
window.add(westPanel, BorderLayout.WEST);
JLabel centerLabel = new JLabel("Center");
JLabel eastLabel = new JLabel("East");
JLabel westLabel = new JLabel("West");
eastPanel.add(eastLabel);
westPanel.add(westLabel);
centerPanel.add(centerLabel);
centerPanel.setPreferredSize(new Dimension(200, 400));
eastPanel.setPreferredSize(new Dimension(100, 400));
westPanel.setPreferredSize(new Dimension(100, 400));
eastPanel.setBackground(new Color(200, 240, 200));
westPanel.setBackground(new Color(200, 240, 0));
window.setVisible(true);
A third panel is never created and nested within the other panel.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestCode_Web {
public static void main(String[] args) {
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 400);
JPanel innerPanel = new JPanel(new BorderLayout());
JPanel panelUp = new JPanel(new BorderLayout());
JPanel panelUpLeft = new JPanel();
JPanel panelUpMiddle = new JPanel();
JPanel panelUpRight = new JPanel(); //Create third panel
window.add(innerPanel, BorderLayout.NORTH);
innerPanel.add(panelUp, BorderLayout.NORTH);
panelUp.add(panelUpRight, BorderLayout.EAST); //Add Third Panel
panelUp.add(panelUpLeft, BorderLayout.WEST);
panelUp.add(panelUpMiddle, BorderLayout.CENTER);
JLabel label1 = new JLabel("Label 1");
JLabel label11 = new JLabel("Label 11");
JLabel label12 = new JLabel("Label 12");
panelUpRight.add(label1); //Add label for third panel
panelUpLeft.add(label11);
panelUpMiddle.add(label12);
panelUp.setBackground(new Color(200, 240, 200));
panelUpLeft.setBackground(new Color(200, 240, 0));
panelUpMiddle.setBackground(new Color(100, 240, 200));
panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));
panelUpLeft.setPreferredSize(new Dimension(160, 120));
window.setVisible(true);
}
}
Actually, you don't need the innerPanel:
public class TestCode_Web
{
public static void main(String[] args) {
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
window.setSize(400, 400);
JPanel panelUp = new JPanel();
JPanel panelUpLeft = new JPanel();
JPanel panelUpMiddle = new JPanel();
window.add(panelUp, BorderLayout.NORTH);
window.add(panelUpLeft, BorderLayout.WEST);
window.add(panelUpMiddle, BorderLayout.CENTER);
JLabel label1 = new JLabel("Label 1");
JLabel label11 = new JLabel("Label 11");
JLabel label12 = new JLabel("Label 12");
panelUp.add(label1);
panelUpLeft.add(label11);
panelUpMiddle.add(label12);
panelUp.setBackground(new Color(200, 240, 200));
panelUpLeft.setBackground(new Color(200, 240, 0));
panelUpMiddle.setBackground(new Color(100, 240, 200));
panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));
panelUpLeft.setPreferredSize(new Dimension(160, 120));
window.setVisible(true);
}
}
NiceCow gave the answer why yours is not working. This should work fine.
How can I get rid of that gray box?
This is what I'm talking about:
i would really appreciate if you could help me out
Full code here: http://pastebin.com/nrpCTjvV
public final void initUI() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(new EmptyBorder(new Insets(90, 155, 40, 60)));
JButton NewGame = new JButton ("New Game!");
JButton Highscore = new JButton("Highscore");
JButton Credits = new JButton ("Credits");
JButton Website = new JButton ("Website");
JButton Exit = new JButton ("Exit");
panel.add(NewGame);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Highscore);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Credits);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Website);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Exit);
final ButtonGroup entreeGroup = new ButtonGroup();
JRadioButton radioButton;
panel.add(radioButton = new JRadioButton("Music1"));
radioButton.setActionCommand("Music1");
entreeGroup.add(radioButton);
panel.add(radioButton = new JRadioButton("Music2"));
radioButton.setActionCommand("Music2");
entreeGroup.add(radioButton);
panel.add(radioButton = new JRadioButton("No Music", true));
radioButton.setActionCommand("No Music");
entreeGroup.add(radioButton);
add(panel);
pack();
setTitle("Title");
JLabel background = new JLabel(new ImageIcon("background.png"));
add(background);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setSize(400, 400);
}
add(panel);
pack();
setTitle("Title");
JLabel background = new JLabel(new ImageIcon("background.png"));
add(background);
The default layout manager for a JFrame is a BorderLayout. When you add a component without specifying a constraint the component is added to the CENTER. You can't add multiple components to a single location.
Instead you need to use a different component as the background. Then you add your panel to this component. Check out Background Panel. Then code would be something like:
Background background = new BackgroundPanel(...);
background.add(panel);
add(background);
setResizable(false);
pack();
...