nightwissh nightwissh - 6 months ago 10
Java Question

JPane inside JFrame

I'm trying to make simple app in Java but I have problem with inserting JPane (pasy) inside JFrame(frame). For now my output is two windows, one with resisor and second with combo boxes. Can you please explain me what am I doing wrong? I also tried

frame.getContentPane().add(pasy);
but it didn't work.

import java.awt.* ;
import java.awt.Color;
import java.awt.event.* ;
import java.awt.Frame ;
import java.awt.Graphics;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Map;

public class rezystorGUI extends JFrame implements ActionListener {
String kod="",kolor="",pasek1="BLACK",pasek2="WHITE",pasek3="VIOLET",pasek4="WHITE",pasek5="BLACK";
JComboBox p1, p2, p3, p4, p5;
JTextField wynik;
ResistorColorCode rcc;
WpiszWartosc wwart;
/** * Constructor for objects of class REZYSTORGUI */
rezystorGUI(){
setTitle("Odczytywanie paskow rezystora");
setSize(375,300);
setVisible(true);
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
e.getWindow().dispose();
}
});
rcc = new ResistorColorCode();
makeGUI() ;
}
/** method */
void makeGUI() {
JFrame frame = new JFrame();
frame.setSize(375,300);

JPanel pasy = new JPanel();
pasy.setLayout(new GridLayout(3,3));
pasy.setSize(200,200);

String[]pasek = {"BLACK","BROWN","RED","ORANGE","YELLOW","GREEN","BLUE","VIOLET","GRAY","WHITE"};
String[]multi = {"BLACK","BROWN","RED","ORANGE","YELLOW","GREEN","BLUE","VIOLET"};
String[]tol = {"BROWN","RED","GREEN","BLUE","VIOLET","GRAY","GOLD","SILVER"};

pasy.add(p1 = new JComboBox<String>(pasek));
p1.addActionListener(this);
pasy.add(p2 = new JComboBox<String>(pasek));
p2.addActionListener(this);
pasy.add(p3 = new JComboBox<String>(pasek));
p3.addActionListener(this);
pasy.add(p4 = new JComboBox<String>(multi));
p4.addActionListener(this);
pasy.add(p5 = new JComboBox<String>(tol));
p5.addActionListener(this);
wynik= new JTextField(30);
pasy.add(wynik);

frame.add(pasy);
frame.pack();
frame.setVisible(true);
}

@Override
public void paint(Graphics g) {
super.paint(g);
Map<String, Color> colors = new HashMap<String, Color>();
colors.put("BLUE", Color.BLUE);
colors.put("RED", Color.RED);
colors.put("GREEN", Color.GREEN);
colors.put("WHITE", Color.WHITE);
colors.put("YELLOW", Color.YELLOW);
colors.put("BLACK", Color.BLACK);
colors.put("ORANGE", Color.ORANGE);
colors.put("GRAY", Color.GRAY);
colors.put("VIOLET", new Color(127,0,255));
colors.put("BROWN", new Color(150,75,0));
colors.put("GOLD", new Color(255,215,0));
colors.put("SILVER", new Color(192,192,192));
colors.put("LIGHT-BLUE", new Color(153,204,255));

int x=100;
g.setColor(colors.get("GRAY"));
g.fillRect(40, 95, 60, 10);
g.setColor(colors.get("GRAY"));
g.fillRect(40, 95, 10, 50);

g.setColor(colors.get("LIGHT-BLUE"));
g.fillRect(x, 40, 20, 120);

g.setColor(colors.get(pasek1));
x=x+20;
g.fillRect(x, 40, 10, 120);

g.setColor(colors.get("LIGHT-BLUE"));
x=x+10;
g.fillRect(x, 40, 10, 120);

g.setColor(colors.get("LIGHT-BLUE"));
x=x+10;
g.fillRect(x, 50, 95, 100);

g.setColor(colors.get(pasek2));
x=x+5;
g.fillRect(x, 50, 10, 100);

g.setColor(colors.get(pasek3));
x=x+30;
g.fillRect(x, 50, 10, 100);

g.setColor(colors.get(pasek4));
x=x+30;
g.fillRect(x, 50, 10, 100);

g.setColor(colors.get("LIGHT-BLUE"));
x=x+30;
g.fillRect(x, 40, 10, 120);

g.setColor(colors.get(pasek5));
x=x+10;
g.fillRect(x, 40, 10, 120);

g.setColor(colors.get("LIGHT-BLUE"));
x=x+10;
g.fillRect(x, 40, 20, 120);

g.setColor(colors.get("GRAY"));
x=x+20;
g.fillRect(x, 95, 60, 10);
x=x+50;
g.setColor(colors.get("GRAY"));
g.fillRect(x, 95, 10, 50);
}

public void actionPerformed(ActionEvent e){
Object eventSource = e.getSource();
if (eventSource == p1){
pasek1 = (String)p1.getSelectedItem();
kod+=pasek1;
repaint();
}
if (eventSource == p2){
pasek2 = (String)p2.getSelectedItem();
kod+="-";
kod+=pasek2;
repaint();
}
if (eventSource == p3){
pasek3 = (String)p3.getSelectedItem();
kod+="-";
kod+=pasek3;
repaint();
}
if (eventSource == p4){
pasek4 = (String)p4.getSelectedItem();
kod+="-";
kod+=pasek4;
repaint();
}
if (eventSource == p5){
pasek5 = (String)p5.getSelectedItem();
String tempkod;
tempkod=kod;
kod+="-";
kod+=pasek5;
repaint();
rcc.inputColorCode(kod);
kod=tempkod;
wynik.setText(rcc.convertToValues()+" Ohm(s)\n"+rcc.tolerancja+" tolerancji");
}
}

public static void main(String[] args)
{
new rezystorGUI() ;
}
}

Answer

Your output is two JFrames because you're creating two JFrames. One is the class which extends JFrame:

public class rezystorGUI extends JFrame

and the other is the frame variable:

void makeGUI() {
    JFrame frame = new JFrame();

Solution: don't do this, simple as that. Myself, I avoid extending JFrame, since for one it helps prevent me from doing bad things, like painting directly in the JFrame (like you're doing!).

Instead have your class extend JPanel, draw within its paintComponent method, just as the Swing painting tutorials recommend that you do, add all your components to it, and then when you need to display it in a stand alone GUI, create a JFrame, add your class to its contentPane, pack, and display the JFrame.

e.g.,

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.*;

public class Resistor extends JPanel {
    private String kod = "";
    private String kolor = "";
    private String pasek1 = "BLACK";
    private String pasek2 = "WHITE";
    private String pasek3 = "VIOLET";
    private String pasek4 = "WHITE";
    private String pasek5 = "BLACK";
    private String[] paseks = {pasek1, pasek2, pasek3, pasek4, pasek5};
    private JLabel imageLabel = new JLabel();
    private List<JComboBox<String>> combos = new ArrayList<>();
    private JTextField textField = new JTextField(10);

    public Resistor() {
        imageLabel.setIcon(createIcon());

        String[] pasek = { "BLACK", "BROWN", "RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "VIOLET",
                "GRAY", "WHITE" };
        String[] multi = { "BLACK", "BROWN", "RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "VIOLET" };
        String[] tol = { "BROWN", "RED", "GREEN", "BLUE", "VIOLET", "GRAY", "GOLD", "SILVER" };

        String[][] comboModels = {pasek, pasek, pasek, multi, tol};

        JPanel comboPanel = new JPanel(new GridLayout(2, 3));
        for (int i = 0; i < comboModels.length; i++) {
            JComboBox<String> combo = new JComboBox<>(comboModels[i]);
            combo.addActionListener(new ComboListener());
            comboPanel.add(combo);
            combos.add(combo);
        }
        comboPanel.add(textField);

        setLayout(new BorderLayout());
        add(imageLabel, BorderLayout.CENTER);
        add(comboPanel, BorderLayout.PAGE_END);
    }

    private Icon createIcon() {
        String[] pasek = { "BLACK", "BROWN", "RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "VIOLET",
                "GRAY", "WHITE" };
        String[] multi = { "BLACK", "BROWN", "RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "VIOLET" };
        String[] tol = { "BROWN", "RED", "GREEN", "BLUE", "VIOLET", "GRAY", "GOLD", "SILVER" };
        Map<String, Color> colors = new HashMap<String, Color>();
        colors.put("BLUE", Color.BLUE);
        colors.put("RED", Color.RED);
        colors.put("GREEN", Color.GREEN);
        colors.put("WHITE", Color.WHITE);
        colors.put("YELLOW", Color.YELLOW);
        colors.put("BLACK", Color.BLACK);
        colors.put("ORANGE", Color.ORANGE);
        colors.put("GRAY", Color.GRAY);
        colors.put("VIOLET", new Color(127, 0, 255));
        colors.put("BROWN", new Color(150, 75, 0));
        colors.put("GOLD", new Color(255, 215, 0));
        colors.put("SILVER", new Color(192, 192, 192));
        colors.put("LIGHT-BLUE", new Color(153, 204, 255));

        int w = 375;
        int h = 200;
        BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = img.createGraphics();

        int x = 100;
        g.setColor(colors.get("GRAY"));
        g.fillRect(40, 95, 60, 10);
        g.setColor(colors.get("GRAY"));
        g.fillRect(40, 95, 10, 50);

        g.setColor(colors.get("LIGHT-BLUE"));
        g.fillRect(x, 40, 20, 120);

        g.setColor(colors.get(pasek1));
        x = x + 20;
        g.fillRect(x, 40, 10, 120);

        g.setColor(colors.get("LIGHT-BLUE"));
        x = x + 10;
        g.fillRect(x, 40, 10, 120);

        g.setColor(colors.get("LIGHT-BLUE"));
        x = x + 10;
        g.fillRect(x, 50, 95, 100);

        g.setColor(colors.get(pasek2));
        x = x + 5;
        g.fillRect(x, 50, 10, 100);

        g.setColor(colors.get(pasek3));
        x = x + 30;
        g.fillRect(x, 50, 10, 100);

        g.setColor(colors.get(pasek4));
        x = x + 30;
        g.fillRect(x, 50, 10, 100);

        g.setColor(colors.get("LIGHT-BLUE"));
        x = x + 30;
        g.fillRect(x, 40, 10, 120);

        g.setColor(colors.get(pasek5));
        x = x + 10;
        g.fillRect(x, 40, 10, 120);

        g.setColor(colors.get("LIGHT-BLUE"));
        x = x + 10;
        g.fillRect(x, 40, 20, 120);

        g.setColor(colors.get("GRAY"));
        x = x + 20;
        g.fillRect(x, 95, 60, 10);
        x = x + 50;
        g.setColor(colors.get("GRAY"));
        g.fillRect(x, 95, 10, 50);

        g.dispose();
        return new ImageIcon(img);
    }

    private class ComboListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO finish!

        }
    }

    private static void createAndShowGui() {
        Resistor mainPanel = new Resistor();

        JFrame frame = new JFrame("Resistor");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}