2016-06-04 4 views
1

Ich versuche, eine einfache App in Java zu machen, aber ich habe Probleme beim Einfügen von JPane (pasy) in JFrame (frame). Momentan ist meine Ausgabe zwei Fenster, eines mit Resisor und das andere mit Comboboxen. Kannst du mir bitte erklären, was ich falsch mache? Ich habe auch versucht frame.getContentPane().add(pasy);, aber es hat nicht funktioniert.JPane in JFrame

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

Antwort

1

Ihr Ausgang ist zwei JFrames weil Sie zwei JFrames zu schaffen. Eine davon ist die Klasse, die JFrame erweitert:

public class rezystorGUI extends JFrame 

und die andere ist die Framevariable:

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

Lösung: dies nicht tun, einfach. Ich selbst vermeide es, JFrame zu erweitern, da es zum einen verhindert, dass ich schlechte Dinge mache, zum Beispiel direkt im JFrame zu malen (wie du es tust!).

Anstatt Ihre Klasse JPanel zu erweitern, zeichnen Sie innerhalb seiner paintComponent Methode, genau wie die Swing Malerei Tutorien empfehlen, dass Sie tun, fügen Sie alle Ihre Komponenten hinzu, und dann, wenn Sie es in einer eigenständigen GUI anzeigen müssen, erstellen ein JFrame, fügen Sie Ihre Klasse zu seinem contentPane hinzu, packen Sie und zeigen Sie den JFrame an.

zB

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

Ich empfehle auch, dass Sie einige Zeit auf der Nicht-GUI „Modell“ Seite der Dinge ausgeben, zum Beispiel, die eine Colorcode-Klasse erstellen, oder besser, eine Enumeration eine, die ein Farbfeld, ein String-Textfeld, ein int-Multiplikatorfeld (das ein Vielfaches von 10 enthält) und vielleicht ein doppeltes Toleranzfeld enthält. Dies würde viel von Ihrem Code vereinfachen und das Debuggen vereinfachen.

+0

danke;) Ihre Antwort hat mich gezwungen, zu malen, ohne JFrame zu erweitern. Jetzt überschreibe ich einfach spezifische JPane, wo ich male, als einfaches Rahmenlayout, um zwei JPanes zu organisieren und es funktioniert: D – nightwissh

+0

@nightwissh: Gern geschehen. Bitte beachte die Antwort, die die Seite "Modell" deines Projekts berührt. –