我的applet布局有问题。它在边框布局中的Canvas旁边有一个List。现在,有时当我运行它时,列表非常窄,51像素宽。其他时候代码微小变化它工作正常,列表足够宽,可以显示条目。该列表从数组中填充,并从数组中注释掉一个项目 足以改变行为。准确地评论这一行可以解决问题
{"---- Lissajous curves ","t","t","1","1"}, // commenting this line fixes problem
我怀疑这是一个错误,但是知道它是否可重复或者我是在做傻事会很好。也不太确定提交错误的正确位置,我在Mac OS X 10.6.8上使用标准Java
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.List;
import javax.swing.SwingUtilities;
public class Zap extends Applet {
private static final long serialVersionUID = 330L;
String[][] equations = new String[][]{
// {"Line ----------------------","3 t-1","t+1"},
{"Line","3 t-1","t+1"},
{"Circle","cos(t)","sin(t)","-pi","pi"},
{"Sq Circle","cos(t)","2 sin(t)","-pi","pi"},
{"Ellipse","2 cos(t)","sin(t)","-pi","pi"},
{"Cycloid","t-sin(t)","1-cos(t)","-20","20"},
{"---- Hypocycloids ----"},
{"Deltoid","2 cos(t)+cos(2t)","2 sin(t)-sin(2t)","-pi","pi"},
{"Astroid","cos(t)^3","sin(t)^3","-pi","pi"},
{"Hypocycloids 5","cos(t) + cos(4 t)/4","sin(t) - sin(4 t)/4","-pi","pi" },
{"---- Epicycloids ----"},
{"Cardioid",
"cos(t) - cos(2 t)/2",
"sin(t) - sin(2 t)/2","-pi","pi" },
{"Nephroid",
"cos(t) - cos(3 t)/3",
"sin(t) - sin(3 t)/3","-pi","pi" },
{"Epicycloid 3",
"cos(t) - cos(4 t)/4",
"sin(t) - sin(4 t)/4","-pi","pi" },
{"Epicycloid 4",
"cos(t) - cos(5 t)/5",
"sin(t) - sin(5 t)/5","-pi","pi" },
{"Epicycloid 4",
"cos(t) - cos(5 t)/5",
"sin(t) - sin(5 t)/5","-pi","pi" },
{"Epicycloid 5",
"cos(t) - cos(6 t)/6",
"sin(t) - sin(6 t)/6","-pi","pi" },
{"---- Lissajous curves ","t","t","1","1"}, // commenting this line
{"Lissajous 1,2","sin(t)","sin(2 t)","-pi","pi"},
{"Lissajous 1,3","sin(t-pi/2)","sin(3 t)","-pi","pi"},
{"Lissajous 1,4","sin(t)","sin(4 t)","-pi","pi"},// {"a Lissajousxxxxxxxxxxb",""},
{"Lissajous 2,1","sin(2t)","sin(t)","-pi","pi"},
{"Lissajous 2,2","sin(2t-pi/4)","sin(2t)","-pi","pi"},
{"Lissajous 2,3","sin(2t-pi/3)","sin(3t)","-pi","pi"},
{"Lissajous 3,4","sin(3t)","sin(4t)","-pi","pi"},
};
/** The canvas for plotting the graph */
private Canvas graphCanvas;
/** List of equations */
List list = new List();
/**
* Initializes the applet FunctionPlotter
*/
@Override
public void init () {
try {
initComponents();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Sets the layout of the applet window to BorderLayout, creates all
* the components and associates them with event listeners if necessary.
* @param j
* @throws JepException
*/
private void initComponents () {
setLayout(new BorderLayout());
// GridBagConstraints gbc1 = new GridBagConstraints(0, 0, 1, 1,0.9, 1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0 , 0) ;
// GridBagConstraints gbc2 = new GridBagConstraints(1, 0, 1, 1, 0.1, 1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);
// setBackground (java.awt.Color.white);
// list.addItemListener(this);
list.setMinimumSize(new Dimension(220, 200));
list.setSize(new Dimension(220, 200));
list.setPreferredSize(new Dimension(220, 200));
System.out.println("list "+list.getBounds());
System.out.println("list ps"+list.getPreferredSize());
//add(list,gbc2);
add("East",list);
for(String[] eles:equations) {
list.add(eles[0]);
}
// create the graph canvas and add it
graphCanvas = createGraphCanvas();
//add (graphCanvas,gbc1);
add("Center",graphCanvas);
System.out.println("list ps before validate"+list.getPreferredSize());
list.validate();
this.validate();
System.out.println("list ps after validate"+list.getPreferredSize());
System.out.println(((Object)list).toString());
System.out.println("\n\n");
}
protected Canvas createGraphCanvas() {
Canvas gc = new Canvas();
gc.setPreferredSize(new Dimension(300,300));
gc.setSize(new Dimension(300,300));
System.out.println("PC size "+gc.size());
return gc;
}
public static void main(String argv[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
protected static void createAndShowGUI() {
Frame f = new Frame();
f.setSize(600, 400);
Zap pp = new Zap();
f.add(pp);
pp.init();
System.out.println("pack");
//f.pack();
System.out.println("setVis");
f.setVisible(true);
System.out.println("setVis done");
}
}
答案 0 :(得分:3)
尝试使用此列表(基于Swing)变体。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
public class Zap extends JPanel {
private static final long serialVersionUID = 330L;
String[][] equations = new String[][]{
// {"Line ----------------------","3 t-1","t+1"},
{"Line","3 t-1","t+1"},
{"Circle","cos(t)","sin(t)","-pi","pi"},
{"Sq Circle","cos(t)","2 sin(t)","-pi","pi"},
{"Ellipse","2 cos(t)","sin(t)","-pi","pi"},
{"Cycloid","t-sin(t)","1-cos(t)","-20","20"},
{"---- Hypocycloids ----"},
{"Deltoid","2 cos(t)+cos(2t)","2 sin(t)-sin(2t)","-pi","pi"},
{"Astroid","cos(t)^3","sin(t)^3","-pi","pi"},
{"Hypocycloids 5","cos(t) + cos(4 t)/4","sin(t) - sin(4 t)/4","-pi","pi" },
{"---- Epicycloids ----"},
{"Cardioid",
"cos(t) - cos(2 t)/2",
"sin(t) - sin(2 t)/2","-pi","pi" },
{"Nephroid",
"cos(t) - cos(3 t)/3",
"sin(t) - sin(3 t)/3","-pi","pi" },
{"Epicycloid 3",
"cos(t) - cos(4 t)/4",
"sin(t) - sin(4 t)/4","-pi","pi" },
{"Epicycloid 4",
"cos(t) - cos(5 t)/5",
"sin(t) - sin(5 t)/5","-pi","pi" },
{"Epicycloid 4",
"cos(t) - cos(5 t)/5",
"sin(t) - sin(5 t)/5","-pi","pi" },
{"Epicycloid 5",
"cos(t) - cos(6 t)/6",
"sin(t) - sin(6 t)/6","-pi","pi" },
{"---- Lissajous curves ----","t","t","1","1"}, // commenting this line
{"Lissajous 1,2","sin(t)","sin(2 t)","-pi","pi"},
{"Lissajous 1,3","sin(t-pi/2)","sin(3 t)","-pi","pi"},
{"Lissajous 1,4","sin(t)","sin(4 t)","-pi","pi"},// {"a Lissajousxxxxxxxxxxb",""},
{"Lissajous 2,1","sin(2t)","sin(t)","-pi","pi"},
{"Lissajous 2,2","sin(2t-pi/4)","sin(2t)","-pi","pi"},
{"Lissajous 2,3","sin(2t-pi/3)","sin(3t)","-pi","pi"},
{"Lissajous 3,4","sin(3t)","sin(4t)","-pi","pi"},
};
/** The canvas for plotting the graph */
private JPanel graphCanvas;
DefaultListModel model = new DefaultListModel();
/** List of equations */
JList list = new JList(model);
/**
* Initializes the FunctionPlotter
*/
public void init () {
try {
initComponents();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Sets the layout of the applet window to BorderLayout, creates all
* the components and associates them with event listeners if necessary.
* @param j
* @throws JepException
*/
private void initComponents () {
setLayout(new BorderLayout());
System.out.println("list "+list.getBounds());
System.out.println("list ps"+list.getPreferredSize());
list.setVisibleRowCount(8);
JScrollPane scroll = new JScrollPane(
list,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
add(BorderLayout.LINE_END,scroll);
for(String[] eles:equations) {
model.addElement(eles[0]);
}
// create the graph canvas and add it
graphCanvas = createGraphCanvas();
//add (graphCanvas,gbc1);
add("Center",graphCanvas);
}
protected JPanel createGraphCanvas() {
JPanel gc = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN.darker());
g.fillRect(0, 0, getWidth(), getHeight());
}
};
gc.setPreferredSize(new Dimension(300,10));
return gc;
}
public static void main(String argv[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
protected static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Zap pp = new Zap();
f.add(pp);
pp.init();
System.out.println("pack");
f.pack();
System.out.println("setVis");
f.setVisible(true);
System.out.println("setVis done");
}
}