2016-05-01 10 views
0

Ich habe viele Stunden an der Erstellung eines neuen JFram mit actionlistener gearbeitet, aber ich kann die Hintergrundfarbe meines Frames nicht ändern. Ich habe Panels, getContentPane() usw. ausprobiert. Momentan zeigt das Buy-Fenster alles an, was ich will, aber der Hintergrund ist weiß und ich habe versucht, es in Grau zu ändern.Ich erstelle einen neuen JFrame mit Aktion Listener, kann aber die Hintergrundfarbe nicht ändern

Unten ist mein Code.

import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JMenuBar; 
import javax.swing.JMenu; 
import javax.swing.JMenuItem; 
import javax.swing.JLabel; 
//import com.jgoodies.forms.factories.DefaultComponentFactory; 
import java.awt.BorderLayout; 
import javax.swing.JPanel; 
import java.awt.FlowLayout; 
import javax.swing.JTextArea; 
import javax.swing.JSplitPane; 
import javax.swing.JTextField; 
import javax.swing.JProgressBar; 
import javax.swing.JButton; 
import javax.swing.JFileChooser; 
import javax.swing.JScrollBar; 
import java.awt.Color; 
import javax.swing.JTable; 
import javax.swing.border.BevelBorder; 
import javax.swing.filechooser.FileNameExtensionFilter; 
import javax.swing.table.DefaultTableModel; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.IOException; 
import java.awt.event.ActionEvent; 

public class GUIroughdraft { 

private JFrame frmBgszStockSimulator; 
/** 
* @wbp.nonvisual location=53,14 
*/ 
//private final JLabel lblBgszStockSimulator = DefaultComponentFactory.getInstance().createTitle("BGSZ Stock Simulator"); 
private JTextField searchBar; 
private JTable table; 
private JTextField displayBox; 
private JButton searchButton; 

/** 
* Launch the application. Testing Comment 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      GUIroughdraft window = new GUIroughdraft(); 
      try { 
       window = new GUIroughdraft(); 
       window.frmBgszStockSimulator.setVisible(true); 


      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public GUIroughdraft() { 
    try { 
     initialize(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

/** 
* Initialize the contents of the frame. 
* @throws IOException 
*/ 
private void initialize() throws IOException 
{ 
    frmBgszStockSimulator = new JFrame(); 
    frmBgszStockSimulator.getContentPane().setBackground(Color.GRAY); 
    frmBgszStockSimulator.setTitle("BGSZ Stock Simulator"); 
    frmBgszStockSimulator.setBounds(100, 100, 802, 510); 
    frmBgszStockSimulator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JMenuBar menuBar = new JMenuBar(); 
    frmBgszStockSimulator.setJMenuBar(menuBar); 

    JMenu mnFile = new JMenu("File"); 
    menuBar.add(mnFile); 

    JMenuItem mntmSave = new JMenuItem("Save"); 
    mnFile.add(mntmSave); 
    mntmSave.addActionListener(new ActionListener() 
    { 

     @Override 
     public void actionPerformed(ActionEvent arg0) 
     { 
      JFileChooser saveFile = new JFileChooser(); 
      saveFile.setSelectedFile(new File(".csv")); 
      saveFile.showSaveDialog(null); 
     } 
    }); 

    JMenuItem mntmLoad = new JMenuItem("Load"); 
    mnFile.add(mntmLoad); 
    mntmLoad.addActionListener(new ActionListener() 
    { 

      @Override 
      public void actionPerformed(ActionEvent arg0) 
      { 
       JFileChooser openFile = new JFileChooser(); 
       openFile.showOpenDialog(null); 
      } 
     }); 

    JMenuItem mntmOptions = new JMenuItem("Options"); 
    mnFile.add(mntmOptions); 

    JMenu mnActions = new JMenu("Actions"); 
    menuBar.add(mnActions); 

    JMenuItem mntmBuy = new JMenuItem("Buy"); 
    mnActions.add(mntmBuy); 
    mntmBuy.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent arg0) 
      { 
       JFrame frmBgszStockSimulatorBuy = new JFrame(); 
       frmBgszStockSimulatorBuy.getContentPane().setBackground(Color.GRAY); 
       frmBgszStockSimulatorBuy.setTitle("Buy Stocks"); 
       frmBgszStockSimulatorBuy.setVisible(true); 
       frmBgszStockSimulatorBuy.setBounds(100, 100, 500, 500); 
       JPanel p = new JPanel(); 
       frmBgszStockSimulatorBuy.getContentPane().add(p); 
       p.setBackground(Color.gray);//To set panel background instead of frames background 
       frmBgszStockSimulatorBuy.add(p); 

       searchBar = new JTextField(); 
       searchBar.setText("Enter your text here"); 
       searchBar.setBounds(12, 300, 300, 22); 
       frmBgszStockSimulatorBuy.getContentPane().add(searchBar); 
       searchBar.setColumns(10); 

       JTextArea displayBox = new JTextArea(); 
       displayBox.setEditable(false); 
       displayBox.setLineWrap(true); 
       displayBox.setWrapStyleWord(true); 
       displayBox.setText("This will be a text field that displays all your actions and information about stocks, purchases, sales, errors, etc."); 

       displayBox.setBounds(12, 47, 312, 317); 
       frmBgszStockSimulatorBuy.getContentPane().add(displayBox);   

      } 


     }); 

    JMenuItem mntmSell = new JMenuItem("Sell"); 
    mnActions.add(mntmSell); 

    JMenu mnHelp = new JMenu("Help"); 
    menuBar.add(mnHelp); 

    JMenuItem mntmInfo = new JMenuItem("Info"); 
    mnHelp.add(mntmInfo); 

    JMenuItem mntmAbout = new JMenuItem("How to Use"); 
    mnHelp.add(mntmAbout); 
    frmBgszStockSimulator.getContentPane().setLayout(null); 

    JScrollBar scrollBar = new JScrollBar(); 
    scrollBar.setBackground(Color.LIGHT_GRAY); 
    scrollBar.setBounds(323, 47, 21, 317); 
    frmBgszStockSimulator.getContentPane().add(scrollBar); 

    JTextArea displayBox = new JTextArea(); 
    displayBox.setEditable(false); 
    displayBox.setLineWrap(true); 
    displayBox.setWrapStyleWord(true); 
    displayBox.setText("This will be a text field that displays all your actions and information about stocks, purchases, sales, errors, etc."); 

    displayBox.setBounds(12, 47, 312, 317); 
    frmBgszStockSimulator.getContentPane().add(displayBox); 

    searchBar = new JTextField(); 
    searchBar.setText("Enter your text here"); 
    searchBar.setBounds(12, 377, 637, 22); 
    frmBgszStockSimulator.getContentPane().add(searchBar); 
    searchBar.setColumns(10); 

    JButton searchButton = new JButton(); 
    searchButton.setText("Search"); 
    searchButton.setBounds(654, 377, 93, 22); 
    frmBgszStockSimulator.getContentPane().add(searchButton); 
    searchButton.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 

      String userInput = searchBar.getText(); 

        GetStockData gd = new GetStockData(); 
        try 
        { 
         gd.pullPriceData(userInput); 

        } 
        catch (IOException er) 
        { 
         er.printStackTrace(); 

        } 

        String output = Double.toString(gd.getPrice());; 
        if(output == "0") 
        { 
         displayBox.setText("NO STOCK FOUND"); 
        } 
        displayBox.setText("The Price for " + userInput + ": $" + output); 
        //get to print in the table. 
        //userInput = stock ticker 
        //ouput = price 
      } 
     }); 

    JProgressBar progressBar = new JProgressBar(); 
    progressBar.setStringPainted(true); 
    progressBar.setValue(75); 
    progressBar.setBounds(50, 412, 586, 14); 
    frmBgszStockSimulator.getContentPane().add(progressBar); 

    table = new JTable(); 
    table.setModel(new DefaultTableModel(
      new Object[][] { 
       {"Stock Name", "Stock Value", "Amount Owned", "Total Value"}, 
       {" BAC", "$13.48", "4", "$53.92"}, 
       {" RIG", "$8.89", "0", "$0.00"}, 
       {" SUNE", "$0.59", "12", "$7.08"}, 
       {" FCX", "$10.29", "2", "$20.58"}, 
       {" PBR", "$5.86", "0", "$0.00"}, 
       {" GE", "$31.83", "0", "$0.00"}, 
       {" VALE", "$4.24", "24", "$101.76"}, 
       {" VRX", "$27.07", "0", "$0.00"}, 
       {" PFE", "$30.07", "0", "$0.00"}, 
       {" CRC", "$1.05", "8", "$8.40"}, 
       {" GGB", "$1.82", "0", "$0.00"}, 
       {" CHK", "$4.01", "6", "$24.06"}, 
       {" T", "$39.37", "0", "$0.00"}, 
       {" F", "$13.35", "5", "$66.75"}, 
       {" WLL", "$7.66", "0", "$0.00"}, 
      }, 
      new String[] { 
       "New column", "New column", "New column", "New column" 
      } 
     )); 
    table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); 
    table.setBounds(350, 51, 395, 313); 
    frmBgszStockSimulator.getContentPane().add(table); 

    JTextArea txtrValue = new JTextArea(); 
    txtrValue.setText("Displays Cash Value"); 
    txtrValue.setLineWrap(true); 
    txtrValue.setEditable(false); 
    txtrValue.setBounds(99, 12, 172, 22); 
    frmBgszStockSimulator.getContentPane().add(txtrValue); 

    JTextArea txtrCurrentPortfolioValue = new JTextArea(); 
    txtrCurrentPortfolioValue.setText("Display Portfolio Value"); 
    txtrCurrentPortfolioValue.setLineWrap(true); 
    txtrCurrentPortfolioValue.setEditable(false); 
    txtrCurrentPortfolioValue.setBounds(376, 12, 206, 22); 
    frmBgszStockSimulator.getContentPane().add(txtrCurrentPortfolioValue); 

    JLabel lblCashValue = new JLabel("Cash Value:"); 
    lblCashValue.setBounds(24, 15, 111, 16); 
    frmBgszStockSimulator.getContentPane().add(lblCashValue); 

    JLabel lblPortfolioValue = new JLabel("Portfolio Value:"); 
    lblPortfolioValue.setBounds(283, 15, 123, 16); 
    frmBgszStockSimulator.getContentPane().add(lblPortfolioValue); 
} 

/* private class customListener implements ActionListener { 
    private GUIroughdraft window; 

    public customListener(GUIroughdraft window) { 
     this.window = window; 
    } 

    public void actionPerformed(ActionEvent e) { 
    String userInput = window.searchBar.getText(); 
      GetStockData gd = new GetStockData(); 
      try { 
       gd.pullPriceData(userInput); 
      } catch (IOException er) { 
       er.printStackTrace(); 
      } 
      String output = Double.toString(gd.getPrice()); 
      window.displayBox.setText("The Price for " + userInput + ":" + output); 
    } 
}*/ 
+0

ist es besser, nur Pakete mit allen Klassen zu importieren: import javax.swing. *; import java.awt.event. *; import java.awt. *; – GlacialMan

+0

Standardfarbe aller Felder, und Rahmen sind grau, Farbe ändern – GlacialMan

+1

Ich würde nicht mit @GlacialMan übereinstimmen, dass es besser ist, Pakete zu importieren. Wenn Sie Klassen importieren (und die IDE es automatisiert), ist es viel einfacher herauszufinden, wo Sie Verweise auf eine bestimmte Klasse mit 'git grep' oder ähnlichem haben. Natürlich, wenn Sie keine IDE haben, ist es wahrscheinlich mühsam, dies zu tun. – AlBlue

Antwort

2

Das Problem ist mit dem LayoutManager, den Sie verwenden. Der Standard-Layout-Manager für Frame ist BorderLayout. Wenn Sie die AnzeigeBox zu frmBgszStockSimulatorBuy in der Anweisung frmBgszStockSimulatorBuy.getContentPane().add(displayBox) hinzugefügt haben. Es wurde in der Mitte des Rahmens mit der Bedingung BorderLayout.CENTER hinzugefügt. Daher wurde die Größe auf die volle Größe des Frames angepasst, sobald die Größe des Frames geändert wurde. Wenn ich die Anweisung geändert, wie mit dem zweiten Parameter als PAGE_CHART angegeben, wird die displaybox bei PAGE_START hinzugefügt:

frmBgszStockSimulatorBuy.getContentPane().add(displayBox, BorderLayout.PAGE_START); 

Sie die Farbe Grau sehen können, dass Sie nach dieser Änderung festgelegt. Ändern Sie den Code wie oben, um die Hintergrundfarbe zu sehen, die Sie auf den Rahmen angewendet haben. Sie können mehr über das BorderLayout here lesen. Hier ist der vollständige Code, den ich getestet habe. Ich habe einen Teil Ihres Codes kommentiert, den ich nicht kompilieren konnte.

package org.example; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.IOException; 

import javax.swing.JButton; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JProgressBar; 
import javax.swing.JScrollBar; 
import javax.swing.JTable; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 
import javax.swing.border.BevelBorder; 
import javax.swing.table.DefaultTableModel; 

public class GUIroughdraft { 

private JFrame frmBgszStockSimulator; 
/** 
* @wbp.nonvisual location=53,14 
*/ 
//private final JLabel lblBgszStockSimulator = DefaultComponentFactory.getInstance().createTitle("BGSZ Stock Simulator"); 
private JTextField searchBar; 
private JTable table; 
private JTextField displayBox; 
private JButton searchButton; 

/** 
* Launch the application. Testing Comment 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      GUIroughdraft window = new GUIroughdraft(); 
      try { 
       window = new GUIroughdraft(); 
       window.frmBgszStockSimulator.setVisible(true); 


      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public GUIroughdraft() { 
    try { 
     initialize(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

/** 
* Initialize the contents of the frame. 
* @throws IOException 
*/ 
private void initialize() throws IOException 
{ 
    frmBgszStockSimulator = new JFrame(); 
    frmBgszStockSimulator.getContentPane().setBackground(Color.WHITE); 
    frmBgszStockSimulator.setTitle("BGSZ Stock Simulator"); 
    frmBgszStockSimulator.setBounds(100, 100, 802, 510); 
    frmBgszStockSimulator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JMenuBar menuBar = new JMenuBar(); 
    frmBgszStockSimulator.setJMenuBar(menuBar); 

    JMenu mnFile = new JMenu("File"); 
    menuBar.add(mnFile); 

    JMenuItem mntmSave = new JMenuItem("Save"); 
    mnFile.add(mntmSave); 
    mntmSave.addActionListener(new ActionListener() 
    { 

     @Override 
     public void actionPerformed(ActionEvent arg0) 
     { 
      JFileChooser saveFile = new JFileChooser(); 
      saveFile.setSelectedFile(new File(".csv")); 
      saveFile.showSaveDialog(null); 
     } 
    }); 

    JMenuItem mntmLoad = new JMenuItem("Load"); 
    mnFile.add(mntmLoad); 
    mntmLoad.addActionListener(new ActionListener() 
    { 

      @Override 
      public void actionPerformed(ActionEvent arg0) 
      { 
       JFileChooser openFile = new JFileChooser(); 
       openFile.showOpenDialog(null); 
      } 
     }); 

    JMenuItem mntmOptions = new JMenuItem("Options"); 
    mnFile.add(mntmOptions); 

    JMenu mnActions = new JMenu("Actions"); 
    menuBar.add(mnActions); 

    JMenuItem mntmBuy = new JMenuItem("Buy"); 
    mnActions.add(mntmBuy); 
    mntmBuy.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent arg0) 
      { 
       JFrame frmBgszStockSimulatorBuy = new JFrame(); 
       frmBgszStockSimulatorBuy.getContentPane().setBackground(Color.RED); 
       frmBgszStockSimulatorBuy.setTitle("Buy Stocks"); 
       frmBgszStockSimulatorBuy.setVisible(true); 
       frmBgszStockSimulatorBuy.setBounds(100, 100, 500, 500); 
//    JPanel p = new JPanel(); 
//    frmBgszStockSimulatorBuy.getContentPane().add(p); 
//    p.setBackground(Color.gray);//To set panel background instead of frames background 
//    frmBgszStockSimulatorBuy.add(p); 
// 
//    searchBar = new JTextField(); 
//    searchBar.setText("Enter your text here"); 
//    searchBar.setBounds(12, 300, 300, 22); 
//    frmBgszStockSimulatorBuy.getContentPane().add(searchBar); 
//    searchBar.setColumns(10); 
// 
       JTextArea displayBox = new JTextArea(); 
       displayBox.setEditable(false); 
       displayBox.setLineWrap(true); 
       displayBox.setWrapStyleWord(true); 
       displayBox.setText("This will be a text field that displays all your actions and information about stocks, purchases, sales, errors, etc."); 
// 
       displayBox.setBounds(12, 47, 312, 317); 

       frmBgszStockSimulatorBuy.getContentPane().add(displayBox, BorderLayout.PAGE_START); //This is where BorderLayout.PAGE_START is added.  

      } 


     }); 

    JMenuItem mntmSell = new JMenuItem("Sell"); 
    mnActions.add(mntmSell); 

    JMenu mnHelp = new JMenu("Help"); 
    menuBar.add(mnHelp); 

    JMenuItem mntmInfo = new JMenuItem("Info"); 
    mnHelp.add(mntmInfo); 

    JMenuItem mntmAbout = new JMenuItem("How to Use"); 
    mnHelp.add(mntmAbout); 
    frmBgszStockSimulator.getContentPane().setLayout(null); 

    JScrollBar scrollBar = new JScrollBar(); 
    scrollBar.setBackground(Color.LIGHT_GRAY); 
    scrollBar.setBounds(323, 47, 21, 317); 
    frmBgszStockSimulator.getContentPane().add(scrollBar); 

    JTextArea displayBox = new JTextArea(); 
    displayBox.setEditable(false); 
    displayBox.setLineWrap(true); 
    displayBox.setWrapStyleWord(true); 
    displayBox.setText("This will be a text field that displays all your actions and information about stocks, purchases, sales, errors, etc."); 

    displayBox.setBounds(12, 47, 312, 317); 
    frmBgszStockSimulator.getContentPane().add(displayBox); 

    searchBar = new JTextField(); 
    searchBar.setText("Enter your text here"); 
    searchBar.setBounds(12, 377, 637, 22); 
    frmBgszStockSimulator.getContentPane().add(searchBar); 
    searchBar.setColumns(10); 

    JButton searchButton = new JButton(); 
    searchButton.setText("Search"); 
    searchButton.setBounds(654, 377, 93, 22); 
    frmBgszStockSimulator.getContentPane().add(searchButton); 
    searchButton.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 

      String userInput = searchBar.getText(); 

//     GetStockData gd = new GetStockData(); 
//     try 
//     { 
//      gd.pullPriceData(userInput); 
// 
//     } 
//     catch (IOException er) 
//     { 
//      er.printStackTrace(); 
// 
//     } 
// 
//     String output = Double.toString(gd.getPrice());; 
//     if(output == "0") 
//     { 
//      displayBox.setText("NO STOCK FOUND"); 
//     } 
//     displayBox.setText("The Price for " + userInput + ": $" + output); 
        //get to print in the table. 
        //userInput = stock ticker 
        //ouput = price 
      } 
     }); 

    JProgressBar progressBar = new JProgressBar(); 
    progressBar.setStringPainted(true); 
    progressBar.setValue(75); 
    progressBar.setBounds(50, 412, 586, 14); 
    frmBgszStockSimulator.getContentPane().add(progressBar); 

    table = new JTable(); 
    table.setModel(new DefaultTableModel(
      new Object[][] { 
       {"Stock Name", "Stock Value", "Amount Owned", "Total Value"}, 
       {" BAC", "$13.48", "4", "$53.92"}, 
       {" RIG", "$8.89", "0", "$0.00"}, 
       {" SUNE", "$0.59", "12", "$7.08"}, 
       {" FCX", "$10.29", "2", "$20.58"}, 
       {" PBR", "$5.86", "0", "$0.00"}, 
       {" GE", "$31.83", "0", "$0.00"}, 
       {" VALE", "$4.24", "24", "$101.76"}, 
       {" VRX", "$27.07", "0", "$0.00"}, 
       {" PFE", "$30.07", "0", "$0.00"}, 
       {" CRC", "$1.05", "8", "$8.40"}, 
       {" GGB", "$1.82", "0", "$0.00"}, 
       {" CHK", "$4.01", "6", "$24.06"}, 
       {" T", "$39.37", "0", "$0.00"}, 
       {" F", "$13.35", "5", "$66.75"}, 
       {" WLL", "$7.66", "0", "$0.00"}, 
      }, 
      new String[] { 
       "New column", "New column", "New column", "New column" 
      } 
     )); 
    table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); 
    table.setBounds(350, 51, 395, 313); 
    frmBgszStockSimulator.getContentPane().add(table); 

    JTextArea txtrValue = new JTextArea(); 
    txtrValue.setText("Displays Cash Value"); 
    txtrValue.setLineWrap(true); 
    txtrValue.setEditable(false); 
    txtrValue.setBounds(99, 12, 172, 22); 
    frmBgszStockSimulator.getContentPane().add(txtrValue); 

    JTextArea txtrCurrentPortfolioValue = new JTextArea(); 
    txtrCurrentPortfolioValue.setText("Display Portfolio Value"); 
    txtrCurrentPortfolioValue.setLineWrap(true); 
    txtrCurrentPortfolioValue.setEditable(false); 
    txtrCurrentPortfolioValue.setBounds(376, 12, 206, 22); 
    frmBgszStockSimulator.getContentPane().add(txtrCurrentPortfolioValue); 

    JLabel lblCashValue = new JLabel("Cash Value:"); 
    lblCashValue.setBounds(24, 15, 111, 16); 
    frmBgszStockSimulator.getContentPane().add(lblCashValue); 

    JLabel lblPortfolioValue = new JLabel("Portfolio Value:"); 
    lblPortfolioValue.setBounds(283, 15, 123, 16); 
    frmBgszStockSimulator.getContentPane().add(lblPortfolioValue); 
} 
} 

/* private class customListener implements ActionListener { 
    private GUIroughdraft window; 

    public customListener(GUIroughdraft window) { 
     this.window = window; 
    } 

    public void actionPerformed(ActionEvent e) { 
    String userInput = window.searchBar.getText(); 
      GetStockData gd = new GetStockData(); 
      try { 
       gd.pullPriceData(userInput); 
      } catch (IOException er) { 
       er.printStackTrace(); 
      } 
      String output = Double.toString(gd.getPrice()); 
      window.displayBox.setText("The Price for " + userInput + ":" + output); 
    } 
}*/ 
+0

Ich habe die von Ihnen vorgeschlagene Änderung vorgenommen, konnte aber die Farbe Grau nirgendwo auf meinem Fenster sehen. War das die einzige Änderung, die Sie an dieser Codezeile vorgenommen haben? –

+0

Ich habe den Code verwendet, den du gepostet hast, und es hat funktioniert. Vielen Dank. Warum funktioniert dieser Code-Block jedoch nicht? searchBar = neu JTextField(); searchBar.setText ("Geben Sie hier Ihren Text ein"); searchBar.setBounds (12, 300, 300, 22); frmBgszStockSimulatorBuy.getContentPane(). Add (searchBar); searchBar.setColumns (10); –

+0

Sie müssen 'frmBgszStockSimulatorBuy.getContentPane(). Add (searchBar, BorderLayout.PAGE_START);' auch für searchBar angeben. – randominstanceOfLivingThing

0

Standardmäßig sind die Swing-Komponenten transparent. Verwenden Sie setOpaque (true) für die Komponente, deren Hintergrundfarbe geändert werden soll, und dann wird es funktionieren.