小编Hur*_*ler的帖子

如何将文本置于JLabel中心?

尽管经过多次尝试,但我无法得到我想看到的结果 - 文本居中在JLabel中,而JLabel在BorderLayout中居中.我说"有点",因为窗口的右下角应该还有另一个标签"status".这里有一些代码负责:

setLayout(new BorderLayout());
JPanel area = new JPanel();
JLabel text = new JLabel(
        "<html>In early March, the city of Topeka," +
        " Kansas,<br>temporarily changed its name to Google..." +
        "<br><br>...in an attempt to capture a spot<br>" +
        "in Google's new broadband/fiber-optics project." +
        "<br><br><br>source: http://en.wikipedia.org/wiki/Google_server" +
        "#Oil_Tanker_Data_Center</html>", SwingConstants.CENTER);
text.setVerticalAlignment(SwingConstants.CENTER);
JLabel status = new JLabel("status", SwingConstants.SOUTH_EAST);
status.setVerticalAlignment(SwingConstants.CENTER);
Font font = new Font("SansSerif", Font.BOLD, 30);
text.setFont(font);
area.setBackground(Color.darkGray);
text.setForeground(Color.green);
// text.setAlignmentX(CENTER_ALIGNMENT);
// text.setAlignmentY(CENTER_ALIGNMENT);
// text.setHorizontalAlignment(JLabel.CENTER);
// text.setVerticalAlignment(JLabel.CENTER);
Font font2 = new …
Run Code Online (Sandbox Code Playgroud)

java user-interface swing jlabel htmltext

32
推荐指数
3
解决办法
12万
查看次数

使用jxl将数据添加到Excel文件

我正在使用jxl创建一个excel文件.首先应该只包含一些关于样式的格式和信息.然后,每当有人向其添加新数据时,都应该更新它.

public class WriteExcel {

    private static WritableWorkbook workbook;
    private static WritableCellFormat timesStandard;
    private String inputFile;
    final private static int FONT_SIZE = 12;


    public void setOutputFile(String inputFile) {
        this.inputFile = inputFile;
    }

    private void prepareSheet(WritableSheet sheet) throws WriteException {
        sheet.mergeCells(0, 0, 1, 0);
        sheet.mergeCells(3, 0, 4, 0);
        sheet.mergeCells(6, 0, 7, 0);

        WritableFont times12pt = new WritableFont(WritableFont.TIMES, FONT_SIZE);
        timesStandard = new WritableCellFormat(times12pt);

        CellView cv = new CellView();
        cv.setFormat(timesStandard);
    }

    public void write() throws IOException, WriteException {
        File file = new File(inputFile); …
Run Code Online (Sandbox Code Playgroud)

java excel jxl

7
推荐指数
1
解决办法
1万
查看次数

使用 jxl 库使用 Java 向 Excel 文件中的单元格添加注释

我正在尝试向 Excel 中的单元格添加注释。我正在使用 jxl 库来做到这一点:

   cell = sheet.getWritableCell(1, 2); // cols, rows
   WritableCellFeatures wcf = cell.getWritableCellFeatures();
   wcf.setComment("comment2");
Run Code Online (Sandbox Code Playgroud)

最后一行返回:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException。尽管进行了多次尝试,但我无法修复它。帮助将不胜感激。谢谢你。

--EDIT--
这是修改后的addNumber方法:

private static void addNumber(WritableSheet sheet, int column, int row,
        double d) throws WriteException, RowsExceededException {

    Number number;
    number = new Number(column, row, d, timesStandard);

    //sheet.addCell(number); // need to add the cell first

    if (user wants to add a comment) {
        WritableCellFeatures wcf = new WritableCellFeatures();
        wcf.setComment("the comment");
        number.setCellFeatures(wcf);
    }

    //sheet.addCell(number); // but may need …
Run Code Online (Sandbox Code Playgroud)

java excel jxl

5
推荐指数
1
解决办法
4859
查看次数

将其他类的JPanel添加到cardLayout

我在3个单独的类中有3个窗口,我想使用cardLayout,这样当您单击下一个按钮时,将出现下一个窗口.如何将包含不同元素的JPanel添加到一个cardLayout?这是第一个窗口:(唯一不同的是背景 - 但它代表了我实际上是如何得到它的想法)

public class Window1 extends JPanel implements ActionListener {

static CardLayout cardLayout = new CardLayout();

public Window1() {
   init();
}

private void init() {
    JPanel jp = new JPanel(new BorderLayout());
    JPanel jp2 = new Window2();
    //JPanel jp3 = new Window3();
    JLabel textLabel = new JLabel("Window1");
    jp.setBackground(Color.GREEN);
    jp.add(textLabel, BorderLayout.CENTER);
    JButton nextButton = new JButton("NEXT");
    nextButton.setActionCommand("next");
    nextButton.addActionListener(this);
    jp.add(nextButton, BorderLayout.EAST);
    setLayout(cardLayout);
    add(jp, "string");
    add(jp2, "string");
    //add(jp3, "string");
}

public void actionPerformed(ActionEvent e) {            
    if (e.getActionCommand().equalsIgnoreCase("next")) {
    // go to the next …
Run Code Online (Sandbox Code Playgroud)

java user-interface swing jpanel cardlayout

3
推荐指数
1
解决办法
7941
查看次数

格式化文本字段和JComboBox在一起

我有一个GUI窗口,询问休息时间.我想得到的结果是,例如,1:15 - int hours = 1和int mins = 15 - 单击继续按钮后.我得到的结果要么是小时,要么是分钟,因为我不能让JComboBox和JButton一起工作(我想).另外,我不太清楚如何检查用户是输入了数字还是输入了无效的输入.这是代码:

@SuppressWarnings("serial")
public class FormattedTextFields extends JPanel implements ActionListener {

    private int hours;
    private JLabel hoursLabel;
    private JLabel minsLabel;
    private static String hoursString = " hours: ";
    private static String minsString = " minutes: ";
    private JFormattedTextField hoursField;
    private NumberFormat hoursFormat;

    public FormattedTextFields() {

        super(new BorderLayout());
        hoursLabel = new JLabel(hoursString);
        minsLabel = new JLabel(minsString);
        hoursField = new JFormattedTextField(hoursFormat);
        hoursField.setValue(new Integer(hours));
        hoursField.setColumns(10);
        hoursLabel.setLabelFor(hoursField);
        minsLabel.setLabelFor(minsLabel);

        JPanel fieldPane = new JPanel(new GridLayout(0, 2)); …
Run Code Online (Sandbox Code Playgroud)

java user-interface swing combobox jformattedtextfield

3
推荐指数
1
解决办法
1928
查看次数

GUI - 使用CardLayout创建类似向导的窗口

我有几个窗户,以类似巫师的方式.其中一些使用标准的BorderLayout,一些是TableLayout.如果单击"下一步"按钮,则应显示下一个窗口.我读到最好的方法就是使用CardLayout.但我不知道应该如何将它们"链接"在一起.这是第一个窗口:

public class Identification extends JPanel implements ActionListener {

static String next = "Next";
JButton nextButton;
final static int BIG_BORDER = 75;
final static int SMALL_BORDER = 10;
final static int ELEMENTsLENGHT = 320;
final static int VERTICAL_SPACE = 10;
final static int VERTICAL_SPACE_PLUS = 25;
final static int HORIZONTAL_SPACE = 75;
final static int SPACEforELEMENT_LABEL = 50;
final static int SPACEforELEMENT_TEXT = 40;
final static int H_SPACEforBUTTON = 64;
final static int V_SPACEforBUTTON = 26;
private JTextField nameField = …
Run Code Online (Sandbox Code Playgroud)

java user-interface swing tablelayout cardlayout

1
推荐指数
1
解决办法
1329
查看次数