1. ホーム
  2. java

[解決済み] JFrame内のJPane

2022-02-16 01:05:22

質問

Javaで簡単なアプリを作ろうとしているのですが、JFrame(フレーム)の中にJPane(パシ)を挿入するのに問題があります。今のところ、私の出力は2つのウィンドウで、1つはレジスターで、もう1つはコンボボックスです。私は何が間違っているのか説明してもらえますか?私はまた、試してみました frame.getContentPane().add(pasy); が、うまくいきませんでした。

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() ;
   }
}

解決方法は?

出力が2つのJFrameになっているのは、あなたが 作成 の2つのJFrameがあります。1つはJFrameを継承したクラスです。

public class rezystorGUI extends JFrame

で、もうひとつはフレーム変数です。

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

解決策:これをしないこと、簡単なことです。私自身は、JFrameの拡張を避けています。なぜなら、JFrameに直接ペイントするような悪いことを防ぐのに役立つからです(あなたがやっているように!)。

代わりに、あなたのクラスはJPanelを継承し、その中で描画します。 paintComponent メソッドにすべてのコンポーネントを追加し、スタンドアローンのGUIで表示する必要がある場合は、JFrameを作成し、そのcontentPaneにクラスを追加し、パックして、JFrameを表示することを推奨します。

などです。

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();
            }
        });
    }
}


例えば、ColorCodeクラス、またはより良いenumを作成し、Colorフィールド、Stringテキストフィールド、int乗数フィールド(10の倍数を保持)、およびおそらくdoubleトレランスフィールドを保持するものを作成します。こうすることで、コードの多くが簡素化され、デバッグが容易になります。