JFrame

时间:2016-06-04 23:45:34

标签: java swing jframe jpanel

我正在尝试用Java创建简单的应用程序,但我在JFrame(框架)中插入JPane(pasy)时遇到问题。现在我的输出是两个窗口,一个带有电阻器,第二个带有组合盒。你能解释一下我做错了什么吗?我也试过了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() ;
   }
}

1 个答案:

答案 0 :(得分:1)

您的输出是两个JFrame,因为您正在创建两个JFrame。一个是扩展JFrame的类:

public class rezystorGUI extends JFrame

另一个是帧变量:

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

解决方案:不要这样做,这很简单。我自己,我避免扩展JFrame,因为一个它有助于防止我做坏事,比如直接在JFrame中绘画(就像你正在做的那样!)。

相反,你的类扩展JPanel,在其paintComponent方法中绘制,就像Swing绘画教程建议你做的那样,将所有组件添加到它,然后当你需要在独立显示时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();
            }
        });
    }
}

我还建议您花一些时间在非GUI“模型”方面,例如,创建一个ColorCode类,或者更好,一个枚举,一个包含Color字段,一个String文本字段,一个int乘数字段(包含10的倍数),也许是一个双容差字段。这样做可以简化大部分代码并使调试更容易。