Java – 创建股票JPanel时出现问题

huangapple go评论83阅读模式
英文:

Java - Problem during the creation of a stock JPanel

问题

public class AlternateGL_JPanel extends JPanel {
    protected JPanel layoutPanel;
    protected int rows, columns;
    
    public AlternateGL_JPanel(int rows, int columns) { 
        this.rows = rows;
        this.columns = columns;
        
        setBorder(new EmptyBorder(0, 0, 0, 0));
        setLayout(new GridLayout(this.rows, this.columns));
        
        this.layoutPanel = new JPanel(); // This is the nested panel
        layoutPanel.setLayout(new GridLayout(this.rows, this.columns));
        super.add(layoutPanel, BorderLayout.PAGE_START);
    }
    
    public void add(JComponent component) {
        layoutPanel.add(component);
    }
}

/** A <b>ManyTextAndInsertText</b> is a {@link JPanel} that houses a number of {@link JLabel}s to the left 
 * and {@link JTextField}s on the right.
 */
public class ManyTextAndInsertText extends AlternateGL_JPanel {
    private JLabel[] texts;
    private JTextField[] insertTexts;
    
    public ManyTextAndInsertText(String[] descriptions) {
        super(descriptions.length, 2);
        
        this.texts = new JLabel[descriptions.length];
        this.insertTexts = new JTextField[descriptions.length];
        
        for (int i = 0; i < descriptions.length; i++) {
            texts[i] = new JLabel(descriptions[i]);
            insertTexts[i] = new JTextField();
            
            for (int j = 0; j < this.getComponentCount(); j++) {
                System.out.println("\t" + this.getComponent(j).toString());
            }
            
            this.add(texts[i]);
            
            for (int j = 0; j < this.getComponentCount(); j++) {
                System.out.println("\t" + this.getComponent(j).toString());
            }
            
            this.add(insertTexts[i]);
        }
    }
}

public class TestManyTextes extends JFrame {
    private JPanel contentPane;
    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestManyTextes frame = new TestManyTextes();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    public TestManyTextes() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new ManyTextAndInsertText(new String[] { "First text: ", "Second text: " });
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
    }
}
英文:

I am attempting to create a JPanel that houses in a GridLayout a number of JLabels to the left JTextFields on the right. Unfortunately, nothing is shown even if the Components are reported as correctly added. Where is the problem?

public class AlternateGL_JPanel extends JPanel {
protected JPanel layoutPanel;
protected int rows, columns;
public AlternateGL_JPanel(int rows, int columns) { 
this.rows 		= rows;
this.columns 	= columns;
// ===== MAIN FRAME DEFINITION =====
setBorder(new EmptyBorder(0, 0, 0, 0));
setLayout(new GridLayout(this.rows, this.columns));
//	setBounds(10, 10, 500, 500);
// ===== INNER PANEL =====
this.layoutPanel = new JPanel(); 	//This is the nested panel
layoutPanel.setLayout(new GridLayout(this.rows, this.columns));
super.add(layoutPanel, BorderLayout.PAGE_START);
}
// =========================================================			
// TODO | Superclass: JPanel
public void add(JComponent component) {
layoutPanel.add(component);
}
}
/**  A &lt;b&gt;ManyTextAndInsertText&lt;/b&gt; is a {@link JPanel} that houses a number of {@link JLabel}s to the left 
* and {@link JTextField}s on the right.
* 
*/
public class ManyTextAndInsertText extends AlternateGL_JPanel {
private JLabel[] texts;
private JTextField[] insertTexts;
public ManyTextAndInsertText(String[] descriptions) {
super(descriptions.length, 2);
this.texts		 	= new JLabel[descriptions.length];
this.insertTexts 	= new JTextField[descriptions.length];
for(int i=0 ; i&lt;descriptions.length ; i++)
{
texts[i] 		= new JLabel(descriptions[i]);
insertTexts[i]	= new JTextField();
for(int j=0 ; j&lt;this.getComponentCount() ; j++)
System.out.println(&quot;\t&quot; + this.getComponent(j).toString());
this.add(texts[i]);
for(int j=0 ; j&lt;this.getComponentCount() ; j++)
System.out.println(&quot;\t&quot; + this.getComponent(j).toString());
this.add(insertTexts[i]);
}
}
public class TestManyTextes extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestManyTextes frame = new TestManyTextes();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestManyTextes() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new ManyTextAndInsertText(new String[] { &quot;First text: &quot; , &quot;Second text: &quot;});
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}

答案1

得分: 2

我极大地简化了您的代码并创建了这个GUI。

代替扩展Swing组件,我使用了Swing组件。

您可以使用GridBagLayout而不是GridLayout来获得一个更美观的表单。

这是可运行的示例。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class TestManyTexts {

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new TestManyTexts();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestManyTexts() {
        JFrame frame = new JFrame("Test Many Texts");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ManyTexts panel = new ManyTexts(new String[] { "First text: ", 
                "Second text: ", "Third Text:", "Forth Text" });
        frame.add(panel.getPanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public class ManyTexts {

        private JPanel panel;

        private JTextField[] fields;

        public ManyTexts(String[] labels) {
            createPanel(labels);
        }

        private void createPanel(String[] labels) {
            panel = new JPanel(new GridLayout(0, 2));
            panel.setBorder(new EmptyBorder(5, 5, 5, 5));

            fields = new JTextField[labels.length];

            for (int i = 0; i < labels.length; i++) {
                JLabel label = new JLabel(labels[i]);
                panel.add(label);

                fields[i] = new JTextField(15);
                panel.add(fields[i]);
            }
        }

        public JPanel getPanel() {
            return panel;
        }

        public JTextField[] getFields() {
            return fields;
        }

    }

}
英文:

I greatly simplified your code and created this GUI.

Java – 创建股票JPanel时出现问题

Instead of extending Swing components, I used Swing components.

You would get a nicer looking form using a GridBagLayout, rather than a GridLayout.

Here's the runnable example.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class TestManyTexts {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new TestManyTexts();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestManyTexts() {
JFrame frame = new JFrame(&quot;Test Many Texts&quot;);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ManyTexts panel = new ManyTexts(new String[] { &quot;First text: &quot;, 
&quot;Second text: &quot;, &quot;Third Text:&quot;, &quot;Forth Text&quot; });
frame.add(panel.getPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class ManyTexts {
private JPanel panel;
private JTextField[] fields;
public ManyTexts(String[] labels) {
createPanel(labels);
}
private void createPanel(String[] labels) {
panel = new JPanel(new GridLayout(0, 2));
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
fields = new JTextField[labels.length];
for (int i = 0; i &lt; labels.length; i++) {
JLabel label = new JLabel(labels[i]);
panel.add(label);
fields[i] = new JTextField(15);
panel.add(fields[i]);
}
}
public JPanel getPanel() {
return panel;
}
public JTextField[] getFields() {
return fields;
}
}
}

huangapple
  • 本文由 发表于 2020年10月1日 19:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64154796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定