简体   繁体   中英

fixing the size of a JTextField within a GUI

I have the following code to create GUI. The issue i am having is that the JTextField appears to expand as the GUI is resized.

I want the JTextField to stay the same size as the GUI is enlarged. The JTextField also seems to reside at the bottom of the control panel rather than at the top. How do i have control of this?

Can someone provide some helo:

public class HtmlDemo extends JPanel
                      implements ActionListener {
    JLabel theLabel;
    JTextArea htmlTextArea;
    private boolean bLastName = false;
    private boolean bNickName = false;
    private boolean bMarks = false;
    private JFreeChart chart;
    private ChartPanel chartPanel;    
    private JLabel date1Label, date2Label,date3Label,date4Label;
    private JTextField date1Field, date2Field,date3Field,date4Field;
    private JButton button,button1;
    private Container window;
  //  public createChart stackedChart = new createChart();
    public XYPlot plot1;
    private JPanel panel, panel1,panel3,panel4;

    public HtmlDemo() {
        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

      chart = createChart(null);
        chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(1060, 370));
        chartPanel.setMouseZoomable(true, false);
        //panel4.add(chartPanel);

        htmlTextArea = new JTextArea(10, 20);
        //htmlTextArea.setText(initialText);
        JScrollPane scrollPane = new JScrollPane(chartPanel);


        theLabel = new JLabel() {
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
            public Dimension getMinimumSize() {
                return new Dimension(200, 200);
            }
            public Dimension getMaximumSize() {
                return new Dimension(200, 200);
            }
        };
        theLabel.setVerticalAlignment(SwingConstants.CENTER);
        theLabel.setHorizontalAlignment(SwingConstants.CENTER);

        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
        leftPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder(
                    "Rolling Demand"),
                BorderFactory.createEmptyBorder(10,10,10,10)));
        leftPanel.add(scrollPane);
        leftPanel.add(Box.createRigidArea(new Dimension(0,10)));
        //leftPanel.add(changeTheLabel);

        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS));
        rightPanel.setBorder(BorderFactory.createCompoundBorder(
                        BorderFactory.createTitledBorder("Controls"),
                        BorderFactory.createEmptyBorder(10,10,10,10)));
        rightPanel.add(theLabel);


        button = new JButton();
        date1Label = new JLabel("Enter Start Date");
       rightPanel.add(date1Label);
        date1Field = new JTextField(5);
  rightPanel.add(date1Field);
        date2Label = new JLabel("Enter End Date");
      rightPanel.add(date2Label);
        date2Field = new JTextField(5);

      rightPanel.add(date2Field);

       panel1 = new JPanel();
        date3Label = new JLabel("Enter comparator Start Date");
        rightPanel.add(date3Label);
        date3Field = new JTextField(5);
        rightPanel.add(date3Field);
        date4Label = new JLabel("Enter comparator End Date");
        rightPanel.add(date4Label);
        date4Field = new JTextField(5);
        rightPanel.add(date4Field);





      button.setText("Get Data");

        rightPanel.add(button);



        setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        add(leftPanel);
        add(Box.createRigidArea(new Dimension(10,0)));
        add(rightPanel);
    }


     private JFreeChart createChart(final XYDataset dataset)
    {
       // System.out.println("hello");
        return ChartFactory.createTimeSeriesChart(
                "Test",
                "Seconds",
                "Value",
                dataset,
                false,
                false,
                false);
    }

    //React to the user pushing the Change button.
    public void actionPerformed(ActionEvent e) {
        theLabel.setText(htmlTextArea.getText());
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("HtmlDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add content to the window.
        frame.add(new HtmlDemo());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            createAndShowGUI();
            }
        });
    }
}

A Layout controls what gets put where depending on the size. As long as you have a layout, it will always resize. Most of the time, you want this behavior to happen, but to disable simply put

parentContainer.setLayout(null);

This is called a null layout. Note I put parentContainer because you have a bunch of text fields so I don't know which one you want.

Then you can use the textField.setBounds(x, y, width, height) to specify location.

But I do recommend you don't use null layouts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM