如何集成两个不同的jframe?

uqdfh47h  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(270)

我已经准备好了一个生成cpu/时间图的代码(我根据需要更改了另一个代码)。到目前为止还不错,但现在我不能把它集成到一个jframe中,这个jframe真的是我的并且已经定型了!
下面是图表的代码:

/**@see https://stackoverflow.com/a/5048863/230513 */
public class Atol extends ApplicationFrame {

private static final String TITLE = "Dynamic Series";
private static final String START = "Start";
private static final String STOP = "Stop";
private static final float MINMAX = 100;
private static final int COUNT = 2 * 60;
private static final int FAST = 100;
private static final int SLOW = FAST * 5;
private static final Random random = new Random();
private Timer timer;

public Atol(final String title) {
    super(title);
    final DynamicTimeSeriesCollection dataset
            = new DynamicTimeSeriesCollection(1, COUNT, new Second());
    dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 2011));
    dataset.addSeries(gaussianData(), 0, "Gaussian data");
    JFreeChart chart = createChart(dataset);

    final JButton run = new JButton(STOP);
    run.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            if (STOP.equals(cmd)) {
                timer.stop();
                run.setText(START);
            } else {
                timer.start();
                run.setText(STOP);
            }
        }
    });

    final JComboBox combo = new JComboBox();
    combo.addItem("Fast");
    combo.addItem("Slow");
    combo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if ("Fast".equals(combo.getSelectedItem())) {
                timer.setDelay(FAST);
            } else {
                timer.setDelay(SLOW);
            }
        }
    });
   this.add(new ChartPanel(chart), BorderLayout.CENTER);
   JPanel btnPanel = new JPanel(new FlowLayout());
   btnPanel.add(run);
   btnPanel.add(combo);
   this.add(btnPanel, BorderLayout.SOUTH);
    SystemInfo si = new SystemInfo();             //Criando uma nova classe de infos do Sistem
    HardwareAbstractionLayer hal = si.getHardware(); //Infos de Hardware do sistema
    CentralProcessor cpu = hal.getProcessor();      //E as informações da cpu
    long[] oldTricks = cpu.getSystemCpuLoadTicks();

    timer = new Timer(FAST, new ActionListener() {
        float cpu() {

            Double stats = cpu.getSystemCpuLoadBetweenTicks(oldTricks);
            //Convertendo o valor de uso da CPU
            stats = stats * 100d;
            double teste = Math.round(stats * 100.0) / 100.0;
            double d = teste;
            float f = (float) d;
            System.out.println(f);
            return f;
        }
        float[] newData = new float[1];

        @Override
        public void actionPerformed(ActionEvent e) {

            newData[0] = cpu();
            dataset.advanceTime();
            dataset.appendData(newData);
        }
    });
}

private float[] gaussianData() {

    float[] a = new float[COUNT];
    for (int i = 0; i < a.length; i++) {
        a[i] = 2;
    }
    return a;
}

private JFreeChart createChart(final XYDataset dataset) {
    final JFreeChart result = ChartFactory.createTimeSeriesChart(
            TITLE, "hh:mm:ss", "milliVolts", dataset, true, true, false);
    final XYPlot plot = result.getXYPlot();
    ValueAxis domain = plot.getDomainAxis();
    domain.setAutoRange(true);
    ValueAxis range = plot.getRangeAxis();
    range.setRange(-MINMAX, MINMAX);
    return result;
}

public void start() {
    timer.start();
}

public static void main(final String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            Atol demo = new Atol(TITLE);
            demo.pack();

            demo.setVisible(true);
            demo.start();
        }
    });
}}

这是我为按钮编写的代码,该按钮将显示一个带有图表的jpanel:

private void kButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here: GRAFICO DE CPU
cardLayout.show(pnlCards, "cpu");
chartcpu.add(Atol);}

它正好改变了jpanel卡,但是我不能将它插入这个名为chart的jpanel(这个“chart”是另一个jpanel,在card面板中),它会打开一个新窗口!他需要呆在这个“图表”里
在此处输入图像描述
如何调用另一个类中的jpanel chartcpu中的atol类图?
[编辑]成功了,但现在我怎么能把这些装饰品都拿走,只留下图表呢?图表出现了

nzrxty8p

nzrxty8p1#

如果我理解正确的话,你想在另一帧中显示一帧,对吗?如果是这样的话,你可以用jinternalframe

// Define your main frame
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(null);
mainFrame.setSize(800, 800);
mainFrame.setVisible(true);

// Define your subFrame
JInternalFrame subFrame = new JInternalFrame();
subFrame.setLocation(200, 200);
subFrame.setSize(250, 250);        
subFrame.setClosable(false);
subFrame.setResizable(false); 
subFrame.setVisible(true);

// Add your subFrame to mainFrame and repaint
mainFrame.add(subFrame);
mainFrame.repaint();

删除内部框架的边框

subFrame.setBorder(null);        
BasicInternalFrameUI bFrame = (BasicInternalFrameUI)subFrame.getUI();
bFrame.setNorthPane(null);

相关问题