如何在更改jPanels高度后刷新它?在我编译代码之后,它确实显示了它,但是在我单击两次random按钮(我使用random按钮将高度更改为随机数)之后,它并没有以我想要的方式显示它。
还有安托赫的事。jPanel是颠倒的。我该怎么改变呢?
下面是我到目前为止编写的代码:
package learn;
import java.util.Random;
public class Sorting extends javax.swing.JFrame {
/**
* Creates new form Sorting
*/
public Sorting() {
initComponents();
jPanel1.setVisible(false);
jPanel2.setVisible(false);
jPanel3.setVisible(false);
jPanel4.setVisible(false);
jPanel5.setVisible(false);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
randomizingButton = new javax.swing.JButton();
sortingButton = new javax.swing.JButton();
viewer = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jPanel4 = new javax.swing.JPanel();
jPanel5 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
randomizingButton.setText("Randomizing");
randomizingButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
randomizingButtonActionPerformed(evt);
}
});
sortingButton.setText("Sorting");
jPanel1.setBackground(new java.awt.Color(0, 0, 0));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 20, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel2.setBackground(new java.awt.Color(0, 0, 0));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 20, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel3.setBackground(new java.awt.Color(0, 0, 0));
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 20, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel4.setBackground(new java.awt.Color(0, 0, 0));
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 20, Short.MAX_VALUE)
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel5.setBackground(new java.awt.Color(0, 0, 0));
javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
jPanel5.setLayout(jPanel5Layout);
jPanel5Layout.setHorizontalGroup(
jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 20, Short.MAX_VALUE)
);
jPanel5Layout.setVerticalGroup(
jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
javax.swing.GroupLayout viewerLayout = new javax.swing.GroupLayout(viewer);
viewer.setLayout(viewerLayout);
viewerLayout.setHorizontalGroup(
viewerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(viewerLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
viewerLayout.setVerticalGroup(
viewerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, viewerLayout.createSequentialGroup()
.addContainerGap(155, Short.MAX_VALUE)
.addGroup(viewerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPanel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(viewer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(randomizingButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sortingButton)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(randomizingButton)
.addComponent(sortingButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(viewer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void randomizingButtonActionPerformed(java.awt.event.ActionEvent evt) {
int height[] = new int[10];
Random rand = new Random();
for(int i=0; i<5; i++){
height[i] = rand.nextInt(50)+1;
}
jPanel1.setSize(jPanel1.getWidth(), height[0]);
jPanel2.setSize(jPanel2.getWidth(), height[1]);
jPanel3.setSize(jPanel3.getWidth(), height[2]);
jPanel4.setSize(jPanel4.getWidth(), height[3]);
jPanel5.setSize(jPanel5.getWidth(), height[4]);
jPanel1.setVisible(true);
jPanel2.setVisible(true);
jPanel3.setVisible(true);
jPanel4.setVisible(true);
jPanel5.setVisible(true);
// Testing if the height of the bar was set to the height in the array
for(int i=0; i<5; i++){
System.out.println(height[i]);
}
System.out.println("");
System.out.println(jPanel1.getHeight());
System.out.println(jPanel2.getHeight());
System.out.println(jPanel3.getHeight());
System.out.println(jPanel4.getHeight());
System.out.println(jPanel5.getHeight());
repaint();
// revalidate();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Sorting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Sorting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Sorting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Sorting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Sorting().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JButton randomizingButton;
private javax.swing.JButton sortingButton;
private javax.swing.JPanel viewer;
// End of variables declaration
}
尝试使用repaint()方法。
repaint()不直接调用paint()。它调度对中间方法update()的调用。最后,update()调用paint()。
问题内容: 我想在运行时按下按钮时向JFrame中添加一些新组件。到目前为止,该方法仍然有效,但是我必须手动调整窗口大小才能看到新组件。 有什么我可以触发的动作或可以调用的方法来刷新窗口吗?任何帮助表示赞赏。提前致谢。 问题答案: 你必须去框架。如果这样不起作用,您还必须致电
问题内容: 我正在使用Java进行项目,因此需要在单击特定按钮后重新加载整个程序。这该怎么做? 问题答案: 尝试 如果仍然无法使用,请在完成上述步骤后尝试
问题内容: 这是一个困扰我几个小时的问题,我 自己无法找到解决方案…… 我在网上发现了类似的话题,但是我找不到 很好的解释并且尽可能简单的解决方案的完全相同的问题。我也 看过EDT和SwingWorker API文档,但对 我来说太复杂了:( 所以,让我们说清楚。我有一个带有JLabel的简单JFrame,其中 包含我的图像: It’s invoked by this piece of code:
问题内容: 我有一个JList,它显示来自矢量的信息。然后,用户可以从该向量中添加和删除信息。从Vector添加/删除项目时,是否可以刷新JFrame中的JList?目前我正在做.. 但这不会将JList刷新为新内容。我检查了一下,矢量内容等确实发生了变化,但列表没有刷新。为什么?我该如何解决? 问题答案: 您不应该更新Vector。应该直接对ListModel进行更改,然后表将自动重新绘制自身。
我正在尝试实现 J 选项卡面板。在下面的代码中,我提出了一个与我想要实现的非常相似的案例。我通过将JPanel添加到J选项卡面板来创建了一个选项卡。我已经在JPanel中添加了一个J巴顿和JScrollPane。在单击 J 按钮时,我想添加一个新的 JPanel,其中包含一些 JRadio 按钮到 JScrollPane 中。但是,即使在刷新 JScrollPane 或主 JPanel 之后,这些
编辑:当我拖动边框时,某种刷新被发送,我需要弄清楚并手动发送相同的刷新。 请注意,我已经尝试使用revalidate()和repaint()。 当使用JFrame和JPanel来显示一个框架时,我试图使框架的大小易于改变。 null 此外,一个重要的注意事项是,当您拖动边框时,白条会消失,并正确地重新加载/刷新内容(即使您只是少量拖动)