我不明白为什么最后一部分没有添加到我的界面。我尝试了不同的方法,但似乎没有一种方法可行。我无法通过这种方式向我的面板添加1024个标签。我做错了什么,或者有替代方法吗? 可以告诉我代码有什么问题吗?谢谢!
完整代码:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.border.LineBorder;
public class CacheGUI extends JFrame {
private static final int LEN = 1024;
private static final long serialVersionUID = 1L;
private JTextField textField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CacheGUI frame = new CacheGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public CacheGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700, 500);
getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Lista de referinte", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel.setBounds(6, 16, 124, 364);
getContentPane().add(panel);
panel.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(6, 16, 108, 340);
panel.add(scrollPane);
JTextPane txtpnDasd = new JTextPane();
txtpnDasd.setToolTipText("Lista curenta de adrese");
txtpnDasd.setEnabled(false);
txtpnDasd.setEditable(false);
scrollPane.setViewportView(txtpnDasd);
txtpnDasd.setText("<generate first>");
JButton btnGenerate = new JButton("GENERATE");
btnGenerate.setToolTipText("Incepe generarea listei de adrese.");
btnGenerate.setBounds(6, 422, 124, 23);
getContentPane().add(btnGenerate);
JButton btnStart = new JButton("START");
btnStart.setToolTipText("Ruleaza simularea");
btnStart.setBounds(550, 422, 124, 23);
getContentPane().add(btnStart);
JLabel lblBlockSize = new JLabel("Block size:");
lblBlockSize.setFont(new Font("Tahoma", Font.BOLD, 12));
lblBlockSize.setBounds(6, 394, 85, 14);
getContentPane().add(lblBlockSize);
textField = new JTextField();
textField.setToolTipText("");
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setText("4");
textField.setBounds(79, 391, 51, 20);
getContentPane().add(textField);
textField.setColumns(10);
int blocksize = Integer.parseInt(textField.getText());
int nrofblocks = LEN/blocksize; // LEN is 1024
JPanel block_panel = new JPanel();
block_panel.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
block_panel.setBounds(140, 16, 534, 398);
getContentPane().add(block_panel);
block_panel.setLayout(new GridLayout(blocksize,nrofblocks,2,0));
// THIS IS THE PART THAT DOES NOT ADD TO THE PANEL I CREATED BEFORE --->
JLabel[] lbl=new JLabel[LEN];
for(int i=0;i<LEN;i++)
{
lbl[i]=new JLabel("TEST");
lbl[i].setBackground(Color.YELLOW);
lbl[i].setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
block_panel.add(lbl[i]); // i want to create yellow blocks inside my panel as labels to modyfy the color afterwards
}//<----
}
}
答案 0 :(得分:0)
问题是你拥有的标签数量太多了;他们在那里。
将块面板的边界设置为更大的值,减少LEN等,你会看到它们。
答案 1 :(得分:0)
首先要做的事:您的标签不会有黄色背景颜色。默认情况下,JLabel不是不透明的。将其设置为true将显示黄色背景:
lbl[i].setOpaque(true);
第二件事是你拥有的标签数量。将数字减少到100说,你会看到你的标签。
for(int i=0;i<100;i++)
{
lbl[i]=new JLabel("TEST");
lbl[i].setBackground(Color.YELLOW);
lbl[i].setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
lbl[i].setOpaque(true);
block_panel.add(lbl[i]);
}
答案 2 :(得分:0)
使用JScrollPane,设置不透明,网格布局1.rows,2.columns,为标签设置固定大小, 使用BorderFactory。
JPanel block_panel = new JPanel();
block_panel.setBorder(new TitledBorder(null, "", TitledBorder.LEADING,
TitledBorder.TOP, null, null));
block_panel.setBounds(140, 16, 534, 398);
JScrollPane block_scrollpane = new JScrollPane(block_panel);
block_scrollpane.setBounds(140, 16, 534, 398);
getContentPane().add(block_scrollpane);
//block_panel.setLayout(new GridLayout(blocksize, nrofblocks, 2, 0));
block_panel.setLayout(new GridLayout(nrofblocks, blocksize, 2, 0));
// THIS IS THE PART THAT DOES NOT ADD TO THE PANEL I CREATED BEFORE --->
JLabel[] lbl = new JLabel[LEN];
Dimension lblSize = new Dimension(85, 16);
for (int i = 0; i < LEN; i++) {
lbl[i] = new JLabel("TEST" + i);
lbl[i].setOpaque(true);
lbl[i].setBackground(Color.YELLOW);
lbl[i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 1, true));
lbl[i].setPreferredSize(lblSize);
lbl[i].setMinimumSize(lblSize);
block_panel.add(lbl[i]); // i want to create yellow blocks
// inside my panel as labels to modyfy the color afterwards
}//<----