我的專案有問題(可能不止一個..)。
JButton僅當我將滑鼠懸停在組件上時才會顯示組件。
我的專案基本上是從 MySQL 中獲取資料并將其設定在按鈕上。
例如:
- 'Aaa', 1000, jbutton[0] 上的'字母'
- 'Bbb', 50, 'alphabet2' 在 jbutton[1]
等等...
按鈕JPanel位于JScrollPane. (我是故意這樣做的,因為沒有滾動資料就無法放入一個面板中,當我單擊按鈕時,必須彈出一個與單擊按鈕上的資訊相關的新視窗)
我ActionListener在另一個按鈕集上添加了一個“類別”按鈕(雞肉、披薩),以將資料放在我上面提到的按鈕上。當點擊類別按鈕時,根據類別提取資料并在按鈕上一一設定。
我搜索了很多次以解決問題,但我找不到答案。只是假設,它發生是因為我setLayout(null)全部使用了,或者因為在主框架設定為可見之后添加了按鈕。
解決問題很好,但更重要的是,我想知道原因。
請幫我找出這個問題的原因,這樣我下次就不會犯同樣的錯誤,避免不良做法!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class JbuttonNotShowUp extends JFrame implements ActionListener {
JButton chicken, pizza;
JButton[] jbtnArray;
JPanel jp, jpFullofButtons;
JScrollPane jsp;
public JbuttonNotShowUp() {
jp = new JPanel();
jp.setLayout(null);
jpFullofButtons = new JPanel();
jpFullofButtons.setLayout(null);
jsp = new JScrollPane(jpFullofButtons, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
chicken = new JButton("chicken");
pizza = new JButton("pizza");
jp.setBounds(0, 0, 440, 650);
chicken.setBounds(25, 80, 61, 35);
pizza.setBounds(90, 80, 61, 35);
jsp.setBounds(25, 140, 385, 450);
chicken.addActionListener(this);
pizza.addActionListener(this);
jp.add(jsp);
jp.add(chicken);
jp.add(pizza);
add(jp);
setBounds(0, 0, 450, 650);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
JbuttonNotShowUp f = new JbuttonNotShowUp();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton jbutton = (JButton) e.getSource();
jpFullofButtons.removeAll();
DataDAO dao = new DataDAO();
ArrayList<DataVO> list = dao.dataExtract(jbutton.getText());
jbtnArray = new JButton[list.size()];
int y = 0;
for (int i = 0; i < list.size(); i ) {
jbtnArray[i] = new JButton(
list.get(i).getName() "," list.get(i).getPrice() "," list.get(i).getDescription());
jbtnArray[i].setSize(390, 50);
jbtnArray[i].setLocation(0, y);
jbtnArray[i].addActionListener(this);
jpFullofButtons.add(jbtnArray[i]);
y = 50;
}
}
}
uj5u.com熱心網友回復:
問題:
- 您正在使用空布局 - 使用 JPanel 將 JButtons 保存在 JScrollPane 中,這將使滾動窗格無法顯示滾動條和有效滾動
- 您將組件添加到容器(與上面相同的 JPanel)中,而無需告訴 GUI 重新繪制容器,因此組件(添加的 JButton)不會顯示。
jpFullofButtons.repaint();后者通過在向 JPanel 添加組件后呼叫來修復-但JScrollPane 仍然無法正常作業
最好使用一個像樣的布局管理器,這里可能是一個 GridLayout,并在容器上呼叫revalidate()jpFullofButtons repaint()JPanel,在向它添加組件之后。
關于您的MRE嘗試的旁注:它幾乎就在那里,但您仍然留在 DAO 要求以及未定義的類中DataVO,阻止我們應對、粘貼和運行您的代碼。
我的 MRE 示例:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.*;
@SuppressWarnings("serial")
public class JButtonsShowUp extends JPanel {
private JButton reAddButtonsBtn = new JButton("Re-Add Buttons");
private JPanel jpFullofButtons = new JPanel(new GridLayout(0, 1));
private JScrollPane jsp = new JScrollPane(jpFullofButtons);
public JButtonsShowUp() {
reAddButtonsBtn.addActionListener(e -> reAddButtons());
JPanel topPanel = new JPanel();
topPanel.add(reAddButtonsBtn);
jsp.setPreferredSize(new Dimension(385, 450));
int gap = 20;
setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
setLayout(new BorderLayout());
add(topPanel, BorderLayout.PAGE_START);
add(jsp, BorderLayout.CENTER);
}
private void reAddButtons() {
jpFullofButtons.removeAll();
int max = 100;
for (int i = 0; i < max; i ) {
String randomText = "";
for (int j = 0; j < 5; j ) {
char c = (char) ('a' (int) (26 * Math.random()));
randomText = c;
}
double randomPrice = 10 20 * Math.random();
final DataVO2 data = new DataVO2(randomText, randomPrice);
String text = String.format("Text: %sd, Price: $%1.2f", randomText, i, randomPrice);
JButton button = new JButton(text);
button.addActionListener(e -> buttonAction(data));
jpFullofButtons.add(button);
}
jpFullofButtons.revalidate();
jpFullofButtons.repaint();
}
private void buttonAction(DataVO2 data) {
System.out.println("Button pressed: " data);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JButtonsShowUp mainPanel = new JButtonsShowUp();
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
class DataVO2 {
private NumberFormat priceFormat = NumberFormat.getCurrencyInstance(Locale.US);
private String name;
private double price;
public DataVO2(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
String priceText = priceFormat.format(price);
return "DataVO2 [name=" name ", price=" priceText "]";
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/457789.html
上一篇:MySQL8.0.x 版本安裝步驟傻瓜式教程【官方版】
下一篇:底部面板的垂直對齊
