当我最大化我的应用程序时,JPanel内部的图像不会随之调整大小.当窗口最大化时,如何调整JPanel及其内容?
编辑:我正在使用BufferedImage
package gui;
public class Solver {
void solveIt(){
CubeGui.moveThat();
}
}
Run Code Online (Sandbox Code Playgroud)
我试图从这个类访问方法moveThat,但它一直告诉我无法从静态引用访问非静态方法moveThat.我不知道这是一个静态参考?
我正在使用JTable,在表格底部有一个空行,以便能够在表格中添加新行.
在空行中插入或写入数据后,我自动添加一个新的空行.(它假设像Microsoft视觉表一样)
我正在使用java默认行排序器,
问题是我需要空行一直是最后一行!但在对表格进行排序后,它将成为第一行.
DefaultRowSorter类的" compare(int model1,int model2) "方法获取2个行号,如果第一行的值为null,则返回-1,如果第二行的值为null,则返回1.并且通过-1进行DESCENDING以获得反转顺序.
//Treat nulls as < then non-null
if (v1 == null) {
if (v2 == null) {
result = 0;
} else {
result = -1;
}
} else if (v2 == null) {
result = 1;
} else {
result = sortComparators[counter].compare(v1, v2);
}
if (sortOrder == SortOrder.DESCENDING) {
result *= -1;
}
Run Code Online (Sandbox Code Playgroud)
空行被排序为最小值,并且DESCENDING将是第一行(因为mult乘以-1)并且导致很多问题.
我可以覆盖它,并且在DESCENDING模式下,空行(通常是最后一行)不会多次-1,它将是任何排序后的最后一行.但问题是" compare "方法和它的调用者" Row "内部类在DefaultRowSorter中是私有的.
有没有办法避免排空空行并使其始终排在最后一行?
对于家庭作业,我试图paintComponent使用用户输入的变量绘制一个框.我已经能够使用固定数字构建我需要做的事情.我整天都在研究这个问题,但却找不到实现变量的方法.这是我正在研究的精简版:
import javax.swing.*;
import java.awt.*;
public class Problem3 extends JFrame{
public static void main(String[] args) {
int xCoord = Integer.parseInt(
JOptionPane.showInputDialog("Enter an X cord."));
JFrame gd = new JFrame();
gd.setLocationRelativeTo(null);
gd.setSize(300, 300);
gd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gd.setVisible(true);
gd.add(new NewPanel());
}
public int getX(){
return xCoord;
}
}
class NewPanel extends JPanel {
int xCoord = getX();
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(10,10,xCoord,50);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
自从发布在这里以来,我尝试在NewPanel类中使用setter方法
public void setX() {
xCoord = Integer.parseInt(
JOptionPane.showInputDialog("Enter an X cord."));
}
Run Code Online (Sandbox Code Playgroud)
运行这导致一个我从未见过的奇怪错误:我得到一个StackOverFlow错误然后对话框弹出多次/数百次并且不会停止弹出.
编辑3使用气垫船完整的鳗鱼解决方案,我让它工作! …
如何执行这个程序?
// Resolve class BorderLayout
import java.awt.*;
// Resolve class JFrame and JButton
import javax.swing.*;
// Definition of class FrameWithBorderLayout
public class FrameWithBorderLayout extends JFrame {// Attribute
private JButton buttonEast; // The east button
private JButton buttonSouth; // The south button
private JButton buttonWest; // The west button
private JButton buttonNorth; // The north button
private JButton buttonCenter; // The center button
// Constructor
public FrameWithBorderLayout() {
// Call super class constructor with a title
super("Frame With Multiple Buttons");
// …Run Code Online (Sandbox Code Playgroud) 我试图将一个对象(jList1上的选定项目)转换为字符串,但它正在返回null.
我试过了:
Object object1 = jList1.getSelectedValue();
String string1 = object.toString();
Run Code Online (Sandbox Code Playgroud)
&
String string1 = jList1.getSelectedValue().toString();
Run Code Online (Sandbox Code Playgroud)
但他们都为我归还了,我有什么不对吗?
这是按下button1时发生的情况:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Object object1 = jList1.getSelectedValue();
String string1 = object1.toString();
System.out.println(string1)
}
Run Code Online (Sandbox Code Playgroud) 在我的空闲时间里,我一直在学习Java,到目前为止,我对如何正确使用变量一无所知.我的意思是,我应该更喜欢使用实例变量,还是只使用单个方法中的变量.
例如,这是我写的一段代码:
public class arsenalTroop {
String[][] troopStats;
String[][] weaponStats;
String[][] armorStats;
String[][] animalStats;
String[] troops;
String[] weapon;
String[] armor;
String[] animal;
JLabel[] troopsArray;
int troopTotal;
int weaponTotal;
int armorTotal;
int lordTotal;
int animalTotal;
JFrame arsenalLordFrame = new JFrame();
JTextField name = new JTextField();
JLayeredPane lP = new JLayeredPane();
JLayeredPane fP = new JLayeredPane();
JLayeredPane ldP= new JLayeredPane();
JLabel siegeLabel = new JLabel();
JComboBox weaponCB;
JComboBox armorCB;
JComboBox animalCB;
JLabel siegeText = new JLabel();
JButton addWep = new JButton();
JButton …Run Code Online (Sandbox Code Playgroud) 我有一个组合框,你可以在代码中看到从表中获取值.所以在我点击确定后,表格的值会发生变化.如何在不关闭和打开jframe的情况下看到compobox的这些新值?我今天做了很多关于java.awt.EventQueue.invokeLater和其他工作人员的研究,但我不能使它工作,我是java新手和编程通用.所以这是代码:
public class Compo extends JFrame implements ActionListener
{//start of class Compo
//start of variables
private JComboBox<String> CompoBox;
private String array[];
private JButton okButton;
private JPanel panel;
//end of variables
public Compo ()
{//start of Compo method
super("Example");
panel=new JPanel(null);
//table = new String[3];
array= new String[3];
array[0]="alpha";
array[1]="beta";
array[2]="charlie";
CompoBox= new JComboBox<>(array);
CompoBox.setBounds(50, 70, 100, 20);
panel.add(CompoBox);
okButton=new JButton("ok");
okButton.setBounds(50, 120, 70, 30);
okButton.setActionCommand("ok");
okButton.addActionListener(this);
panel.add(okButton);
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 200);
setVisible(true);
}//end of compo method
@Override
public void actionPerformed(ActionEvent …Run Code Online (Sandbox Code Playgroud) 我使用的是JFileChooser和showSaveDialoge()与setSelectionMode(JfileChooser.DIRECTORIES_ONLY)设置在那里预选文件将被保存,它会被调用.
我希望用户能够选择新版本的名称以及放置它的位置.我该怎么做?我还想选择一个默认名称.