I was wondering what went wrong. I've done this before but for some reason I can't get this to work this time.
I designed the project in Netbeans. The most relevant part is the method initTiras, which is suppossed to draw something, but it doesn't. I included the code generated by NetBeans.
Here's the most simplified version of the code, and still it paints nothing.
So I created a new Frame in the proyect. Then I put a JPanel in it (in desing mode). The JPanel's called ElPanel and the JFrame LaFrame.
Here's the main class:
package ElPackage;
public class Resistores {
public static void main(String[] args) {
LaFrame UnaFrame = new LaFrame();
UnaFrame.setVisible(true);
}
}
Inside the LaFrame class:
package ElPackage;
import java.awt.Color;
import java.awt.Graphics;
public class LaFrame extends javax.swing.JFrame {
public LaFrame () {
initComponents();
initTiras();
}
public void initTiras() {
Graphics LasG = ElPanel.getGraphics();
LasG.setColor(Color.WHITE);
LasG.fillRect(10, 10, 30, 30);
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
ElPanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
ElPanel.setBackground(new java.awt.Color(51, 0, 51));
ElPanel.setForeground(new java.awt.Color(255, 255, 255));
javax.swing.GroupLayout ElPanelLayout = new javax.swing.GroupLayout(ElPanel);
ElPanel.setLayout(ElPanelLayout);
ElPanelLayout.setHorizontalGroup(
ElPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
ElPanelLayout.setVerticalGroup(
ElPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ElPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ElPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JPanel ElPanel;
// End of variables declaration
}
Override paintComponent(Graphics g) method of elPanel and add your code there. See for example
Use java naming convention - variables should start from lowercase letter.
Related
Im trying to learn Java and i have now met Jpanels, and since i'm using Netbeans i can use their "constructor" in which i can design the panel i want inside my frame.
With netbeans i made a panel inside my panel, and now i wonder:
How do i draw on a panel inside a panel?
(The panel i want to draw on is EvoPanel)
This is my current design
The dark gray box is the second panel inside the panel, if i try to draw a line from the top left corner to the bottom right i end up with this
My code:
Vindue.java
package vindue;
import javax.swing.*;
/**
*
* #author BE56df
*/
public class Vindue {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
paneler panel = new paneler();
JFrame vindue = new JFrame("NEZ - Evolution Simulator v0.1");
vindue.add(panel);
vindue.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
vindue.setResizable(false);
vindue.pack();
vindue.setVisible(true);
}
}
Paneler.java
package vindue;
import java.awt.*;
import javax.swing.*;
/**
*
* #author BE56df
*/
public class paneler extends javax.swing.JPanel {
/**
* Creates new form paneler
*/
public paneler() {
initComponents();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawLine(0, 0, 750, 550);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
EvoPanel = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jToggleButton1 = new javax.swing.JToggleButton();
setMaximumSize(new java.awt.Dimension(750, 550));
setMinimumSize(new java.awt.Dimension(750, 550));
setPreferredSize(new java.awt.Dimension(750, 550));
EvoPanel.setBackground(new java.awt.Color(200, 200, 200));
EvoPanel.setMaximumSize(new java.awt.Dimension(550, 550));
EvoPanel.setMinimumSize(new java.awt.Dimension(550, 550));
javax.swing.GroupLayout EvoPanelLayout = new javax.swing.GroupLayout(EvoPanel);
EvoPanel.setLayout(EvoPanelLayout);
EvoPanelLayout.setHorizontalGroup(
EvoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 550, Short.MAX_VALUE)
);
EvoPanelLayout.setVerticalGroup(
EvoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 550, Short.MAX_VALUE)
);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel1.setText("Control panel");
jLabel1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jToggleButton1.setText("Auto play");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(EvoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(63, 63, 63)
.addComponent(jLabel1)
.addGap(0, 58, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
.addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(EvoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton1))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JPanel EvoPanel;
private javax.swing.JLabel jLabel1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JToggleButton jToggleButton1;
// End of variables declaration
}
Obviously you paint the line on the main JFrame (paneler panel) when you really wants to paint on EvoPanel. You should create a class EvoPanel (with a paintComponent method) and add after paneler#initComponents.
public paneler() {
initComponents();
EvoPanel evoPanel = new EvoPanel();
add(evoPanel) // properly in a layout
}
Advice: for simple GUI's don't use Mantisse (Netbeans GUI builder tool), it won't give you any benefit.
I am using NetBeans IDE to program a GUI application. It is going to calculate the area/volume of all kinds of primitive 2D/3D shapes.
I have created a new JFrame, inside it - a JTabbedPane, which has two tabs for 2D and 3D shapes.
I plan to create custom jpegs for tabs of each shape, that's not a problem, but I can't really draw shapes inside shape-type tabs (see picture).
There's only one .java file, I'm pasting the contents of that in here. Please help me, I am extremely confused as to how can I get around this.
// #author Žanas Stundys
package geometrinis.projektas;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//
public class PagrindinisLangas extends javax.swing.JFrame {
/* Creates new form PagrindinisLangas */
public PagrindinisLangas() {
initComponents();
}
//
/* This method is called from within the constructor to initialize the form */
//#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jEditorPane1 = new javax.swing.JEditorPane();
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jTabbedPane2 = new javax.swing.JTabbedPane();
jPanel3 = new javax.swing.JPanel();
jPanel4 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jScrollPane1.setViewportView(jEditorPane1);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setResizable(false);
setSize(new java.awt.Dimension(960, 480));
jTabbedPane1.setToolTipText("");
jTabbedPane2.setTabPlacement(javax.swing.JTabbedPane.LEFT);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jTabbedPane2.addTab("", new javax.swing.ImageIcon(getClass().getResource("/Assets/2D/square.png")), jPanel3, "Kvadratas / Stačiakampis"); // NOI18N
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jTabbedPane2.addTab("", new javax.swing.ImageIcon(getClass().getResource("/Assets/2D/circle.png")), jPanel4, "Apskritimas"); // NOI18N
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 876, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 387, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jTabbedPane1.addTab("2D", new javax.swing.ImageIcon(getClass().getResource("/Assets/icon2d.png")), jPanel1); // NOI18N
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jTabbedPane1.addTab("3D", new javax.swing.ImageIcon(getClass().getResource("/Assets/icon3d.png")), jPanel2, ""); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 960, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/* #param args the command line arguments */
public static void main(String args[]){
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new PagrindinisLangas().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JEditorPane jEditorPane1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTabbedPane jTabbedPane2;
// End of variables declaration
}
To add a component that is useful for custom drawing, what I do is this:
Create a new class that exends JPanel
Within the class override paintComponent() method to draw the rectangle or whatever other shapes you want. It is helpful if the constructor sets the preferred size to be non-zero.
Compile the new class.
Open the Design view of the JPanel or JFrame I want to use the JPanel in.
Drag the new class from the Projects tab onto the JFrame / Jpanel you want to use it in. (If you are going to use the same custom Panel multiple times it may be helpful to select the class in the projects tab and right-click for Tools -> Add To Pallet so that it is on the Pallet in the design view. )
Here is an example class to be used this way:
public class PanelWithRectangle extends JPanel {
public PanelWithRectangle() {
setPreferredSize(new Dimension(200,100));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.draw(new Rectangle2D.Double(10, 10,20, 25));
}
}
For 3D Drawing it is somewhat different. Swing doesn't support 3D, so we need to use some JavaFX. For JavaFX instead of extending JPanel extend JFXPanel and instead of overriding paintComponent, create a scene object and use setScene. Also JavaFX methods need to be called on the JavaFX Thread so we need to use Platform.runLater() to have functions called on that thread.
public class PanelWithBox extends JFXPanel {
public PanelWithBox() {
setPreferredSize(new Dimension(200,100));
Platform.runLater(this::initFx);
}
private void initFx() {
Group g = new Group();
Scene scene = new Scene(g);
Box box = new Box(20, 30, 40);
box.setMaterial(new PhongMaterial(Color.RED));
box.getTransforms().addAll(Transform.translate(50, 25), new Rotate(30, new Point3D(1, 1, 1)));
g.getChildren().add(box);
setScene(scene);
}
}
I have a Java application program that when I run it with my IDE (Netbeans) works fine. The application is a simple JFrame with a JScrollPane, JTextArea (as logger) and JButton to perform the main function. The problem is, when I "clean and build" the project and execute it outside from IDE, It freezes itself without reason when I click on the JButton. The JScrollPane change to indertemine mode when the programs runs, but it freezes in the middle!. I tried to execute with the console to see some Exception, but nothing happens.
TestSwingThread.java
package testswingthread;
public class TestSwingThread
{
public static void main(String[] args)
{
DisplayWindow dw = new DisplayWindow();
//FRAME PROPERTIES
dw.setTitle("Test");
dw.setResizable(false);
dw.setLocationRelativeTo(null);
dw.setVisible(true);
}
}
DisplayWindow.java
package testswingthread;
public class DisplayWindow extends javax.swing.JFrame {
public DisplayWindow()
{
initComponents();
}
/**
* This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
mainActionButton = new javax.swing.JButton();
statusBar = new javax.swing.JProgressBar();
jScrollPane2 = new javax.swing.JScrollPane();
logger = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));
mainActionButton.setText("MainAction");
mainActionButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mainActionButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(mainActionButton)
.addGap(0, 0, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(mainActionButton)
.addGap(0, 54, Short.MAX_VALUE))
);
logger.setEditable(false);
logger.setBackground(new java.awt.Color(225, 224, 224));
logger.setColumns(20);
logger.setFont(new java.awt.Font("Courier New", 0, 15)); // NOI18N
logger.setRows(5);
logger.setBorder(javax.swing.BorderFactory.createTitledBorder("Logger"));
logger.setCaretColor(new java.awt.Color(255, 255, 255));
jScrollPane2.setViewportView(logger);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
.addComponent(statusBar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(statusBar, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
.addGap(6, 6, 6))
);
pack();
}// </editor-fold>
private void mainActionButtonActionPerformed(java.awt.event.ActionEvent evt) {
Runnable e = new MainActionButtonActions(this);
Thread process_e = new Thread(e);
process_e.start();
}
public void blockButtons()
{
mainActionButton.setEnabled(false);
}
public void unblockButtons()
{
mainActionButton.setEnabled(true);
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane2;
public javax.swing.JTextArea logger;
private javax.swing.JButton mainActionButton;
public javax.swing.JProgressBar statusBar;
// End of variables declaration
}
MainActionButtonActions.java
package testswingthread;
import javax.swing.JTextArea;
public class MainActionButtonActions implements Runnable
{
private final JTextArea logger;
private final DisplayWindow window;
public MainActionButtonActions(DisplayWindow p_window)
{
window = p_window;
logger = window.logger;
logger.setText(null); //CLEAR THE LOG
}
public void run()
{
exec();
}
public boolean exec()
{
boolean result = false;
window.blockButtons();
window.statusBar.setIndeterminate(true);
appendToLogger("TRY ONE..");
appendToLogger("TRY TWO..");
appendToLogger("TRY THREE..");
window.unblockButtons();
window.statusBar.setIndeterminate(false);
return result;
}
private void appendToLogger(String text)
{
logger.append("\n" + text);
logger.setCaretPosition(logger.getDocument().getLength());
}
}
Regards!
I'm creating a "Presentation" on a jFrame as part of a project and need some help.
I have it starting as a 200, 200 size frame with a button in the middle that says "Let's Start"
then when you click on it, the size of the frame changes. When I click on the button you have to drag the window to make it the already defined size seen as the window stays the original 200 by 200 it doesn't open the window to 1335 by 675.
Here's my code.
package randomGUIs;
import java.awt.Color;
import java.awt.Dimension;
public class Presentation extends javax.swing.JFrame {
public Presentation() {
initComponents();
startPanel.setBackground(Color.blue);
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
startPanel = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Let's Start");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout startPanelLayout = new javax.swing.GroupLayout(startPanel);
startPanel.setLayout(startPanelLayout);
startPanelLayout.setHorizontalGroup(
startPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(startPanelLayout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(jButton1)
.addContainerGap(49, Short.MAX_VALUE))
);
startPanelLayout.setVerticalGroup(
startPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(startPanelLayout.createSequentialGroup()
.addGap(78, 78, 78)
.addComponent(jButton1)
.addContainerGap(77, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(startPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(startPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
startPanel.setSize(new Dimension(1335, 675));
startPanel.setMaximumSize(new Dimension(1335, 675));
startPanel.setMinimumSize(new Dimension(1335, 675));
jButton1.setVisible(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Presentation().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JPanel startPanel;
}
I've tried using startPanel.repaint(); with no luck.
Simply set the frame size with this function.
In order to use this function you must include the relevant package.
import java.awt.Dimension;
...
setSize(new Dimension(1320,220));
Your JFrame won't automagically resize itself unless you tell it to. Something like this:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
startPanel.setSize(new Dimension(1335, 675));
startPanel.setMaximumSize(new Dimension(1335, 675));
startPanel.setMinimumSize(new Dimension(1335, 675));
frame.setSize(1335, 675);
jButton1.setVisible(false);
}
You could also use the pack() function of the JFrame class, depending on what you wanted to do.
I'm using the Netbeans GUI builder, but it's a little confusing now. How can I add an image to a panel? I think i'm doing it correct, but it's not showing up. I think it should be in the init() method, but netbeans does not allow me to change that part of the code. This is the code I added for the image:
//these four lines I added to add the image
imageIcon = new ImageIcon("login_icon.png");
image = new JLabel(imageIcon);
image.setToolTipText("SGS Security");
topPanel.add(image);
My Class starts here:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Login.java
*
* Created on Oct 27, 2009, 8:34:15 PM
*/
package sgs;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class Login extends javax.swing.JFrame {
JLabel image;
ImageIcon imageIcon;
/** Creates new form Login */
public Login() {
initComponents();
//these four lines I added to add the image
imageIcon = new ImageIcon("login_icon.png");
image = new JLabel(imageIcon);
image.setToolTipText("SGS Security");
topPanel.add(image);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
topPanel = new javax.swing.JPanel();
userLabel = new javax.swing.JLabel();
passwordLabel = new javax.swing.JLabel();
connectLabel = new javax.swing.JLabel();
forgotPassLabel = new javax.swing.JLabel();
forgotPassCheckBox = new javax.swing.JCheckBox();
cancelButton = new javax.swing.JButton();
okButton = new javax.swing.JButton();
passwordTextField = new javax.swing.JTextField();
userTextField = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(216, 156, 60));
topPanel.setBackground(new java.awt.Color(28, 90, 198));
javax.swing.GroupLayout topPanelLayout = new javax.swing.GroupLayout(topPanel);
topPanel.setLayout(topPanelLayout);
topPanelLayout.setHorizontalGroup(
topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 406, Short.MAX_VALUE)
);
topPanelLayout.setVerticalGroup(
topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 71, Short.MAX_VALUE)
);
userLabel.setText("User name:");
passwordLabel.setText("Password:");
connectLabel.setText("Connect to SGS");
forgotPassLabel.setText("Forgot password");
forgotPassCheckBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
forgotPassCheckBoxActionPerformed(evt);
}
});
cancelButton.setText("Cancel");
okButton.setText("OK");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
userTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
userTextFieldActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(connectLabel)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(passwordLabel)
.addComponent(userLabel)))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(userTextField)
.addComponent(passwordTextField)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(forgotPassCheckBox)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(forgotPassLabel))
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
.addComponent(topPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(topPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27)
.addComponent(connectLabel)
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(userLabel)
.addComponent(userTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(passwordLabel)
.addComponent(passwordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(forgotPassLabel)
.addComponent(forgotPassCheckBox))
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void forgotPassCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void userTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton cancelButton;
private javax.swing.JLabel connectLabel;
private javax.swing.JCheckBox forgotPassCheckBox;
private javax.swing.JLabel forgotPassLabel;
private javax.swing.JButton okButton;
private javax.swing.JLabel passwordLabel;
private javax.swing.JTextField passwordTextField;
private javax.swing.JPanel topPanel;
private javax.swing.JLabel userLabel;
private javax.swing.JTextField userTextField;
// End of variables declaration
}
Ditch the GUI builder and learn how to create GUIs on your own. That way you spend time learning Java instead of learning an IDE. There is probably some GroupLayout property that is not properly set and since GroupLayout was designed to be used by IDEs and not humans I have no idea what the problem might be.
The other possibility is that the IDE can't find your image. Did you add a System.out.println to diplay the image and make sure its not null.
I suggest you read the section from the Swing tutorial on How to Use Icons for a working example that you can download and test to see if it works. Just replace the icons in the tutorial with your icons to make sure they are found.
Edit:
After having a second look at the code I believe my original suggestion is correct. You attempt to add the label to the panel using a single line of code:
topPanel.add(image);
Look at the code generated by the IDE. The add statements are NOT that simple. If you want to manually add a component after the fact then you need read the section from the tutorial on "How to Use Group Layout" to understand the various constraints and methods used.
Or you need to figure out how to do it in the IDE. Thats why I prefer the do it yourself approach. Then you are responsible for the code, not the IDE.
This is one of the weird behaviors in Java (GUIs). You can manually paint the image on a panel.
Here is what I use:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2= (Graphics2D) g;
if (currentImage != null) {
g2.drawImage(currentImage, null, 0, 0);
}
}
Also you should create a "ImagePanel" component, which is an JPanel that encapsolates the Image.
The middle arguement for drawImage is null, because I do not intend on performing an image operation on it.