显示textPane中paintComponent()的信息

时间:2016-12-14 10:22:15

标签: java user-interface

我有我的GUI:

<?php
session_start();
$base = dirname(dirname(__FILE__));
include($base."/include/db.php");

global $conn;
$trees = [];
$treeBoxes = [];
if(isset($_SESSION['clientId'])) {
    $clientId = $_SESSION['clientId'];
    $query = $conn->prepare("SELECT * FROM ct_trees WHERE client_id=?");
    $query->bind_param('i', $clientId);
    $query->execute();
    $result = $query->get_result();
    if($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $tree_id = $row['id'];
            $tree_name = $row['name'];
            $query = $conn->prepare("SELECT id FROM ct_connections WHERE tree_id=?");
            $query->bind_param('i', $tree_id);
            $query->execute();
            $result2 = $query->get_result();
            $connections = $result2->num_rows;
            array_push($treeBoxes, '<span class="checkbox abc-checkbox abc-checkbox-success">',
                '<input id="tree'.$tree_id.'" type="checkbox" rel="'.$tree_id.'">',
                '<label for="tree'.$tree_id.'">'.$tree_name.'</label>',
                '</span>');
            array_push($trees, ["id" => $tree_id, "name" => $tree_name, "connections" => $connections]);
        }
        //Moved echo outside of while loop.
        if(isset($_GET['json'])) {
            echo json_encode($trees);
        } else {
            echo join("", $treeBoxes);
        }
    }
}
?>

Numbers class:

JTextPane numbers;

JFrame frame = new JFrame("GUI");
Numbers nums = new Nums();
numbers = new JTextPane();
frame.setLayout(new GridLayout(0,3));
JPanel p = new JPanel(new GridLayout(0,1));
frame.add(p);
p.add(new JLabel("Phone numbers: "));
numbers.add(nums);
p.add(numbers);
frame.pack();
frame.setVisible(true);

问题是GUI不会以数字TextPane显示paintComponent中的信息。我该怎么做才能在那里显示文字?

1 个答案:

答案 0 :(得分:0)

虽然可以覆盖 paintComponent(),但应尽可能避免使用它。当您按原样使用提供的组件时,Swing做得非常好。

在您的情况下,我建议从 JTextPane 而不是 JLabel 派生 Numbers 。现在,将 phoneNumbersMap 添加到 String 魔术并通过 JTextPane.setText()进行设置。

我认为你的 TimerCallback 以某种方式在与EDT不同的线程上运行。在这种情况下,请在 SwingUtilities.invokeLater()中包含 setText()调用,以确保Swing可以正确地重新绘制它。

顺便说一句,您已选择使用两个级联的 GridLayouts 。除非这不是由您造成的,尝试减少示例,您可能需要查看我在下面的GUI中使用的 BorderLayout

我改变了你的例子,以便能够执行它并得到一些输出。 这是我的代码提案。

GUI:

JFrame frame = new JFrame("GUI");
Numbers numbers = new Numbers();
frame.setLayout(new BorderLayout());
frame.add(new JLabel("Phone numbers: "), BorderLayout.NORTH);
frame.add(numbers,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);

数:

public class Numbers extends JTextPane implements Timing {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String phoneNumbersMap = "Start";

    private TimerCallback nos;

    public Numbers() {
        setPreferredSize(new Dimension(500, 250));
        nos = new TimerCallback(this, 100L);
        nos.start();
    }

    @Override
    public void tick() {
        phoneNumbersMap += "Test\n";
        String pn = phoneNumbersMap.toString();
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Numbers.this.setText("Phone numbers: \n" + pn);
            }
        });
    }

}

时序界面:

public interface Timing {

    void tick();

}

TimerCallback:

public class TimerCallback {

    Timer tim;
    private long period;
    private Numbers numbers;

    public TimerCallback(Numbers numbers, long period) {
        tim = new Timer(true);
        this.period = period;
        this.numbers = numbers;
    }

    public void start() {
        tim.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                TimerCallback.this.numbers.tick();
            }
        }, new Date(), period);
    }

}