gpt4 book ai didi

java - Android 喜欢 Toast in Swing

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:58:44 25 4
gpt4 key购买 nike

我正在尝试在我的 Swing 应用程序中开发类似 Toast (Android) 的功能。作为一个独立的,它工作完美。但是当集成到应用程序中时,它会带来问题。

类文件是:

import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import net.mindcrew.utils.LayoutHelper.Packer;

public class Toast extends JDialog {

String text;

public Toast(String text) {
this.text = text;
initComponents();
}

private void initComponents(){
setLayout(new GridBagLayout());
addComponentListener(new ComponentAdapter() {
// Give the window an rounded rect shape. LOOKS GOOD
// If the window is resized, the shape is recalculated here.
@Override
public void componentResized(ComponentEvent e) {
setShape(new RoundRectangle2D.Double(0,0,getWidth(),getHeight(),50,50));
}
});

setUndecorated(true);
setSize(300,100);
setLocationRelativeTo(null);
getContentPane().setBackground(Color.BLACK);

// Determine what the GraphicsDevice can support.
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
final boolean isTranslucencySupported =
gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT);

//If shaped windows aren't supported, exit.
if (!gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)) {
System.err.println("Shaped windows are not supported");
}

//If translucent windows aren't supported,
//create an opaque window.
if (!isTranslucencySupported) {
System.out.println(
"Translucency is not supported, creating an opaque window");
}

// Set the window to 70% translucency, if supported.
if (isTranslucencySupported) {
setOpacity(0.9f);
}

ImageIcon loading = new ImageIcon(Toast.class.getResource("/net/mindcrew/utils/userinterface/resources/loading-photo.gif"));

JLabel label = new JLabel(text);
label.setForeground(Color.WHITE);
label.setIcon(loading);
Packer packer = new Packer(this);
packer.pack(label).fillboth().west().inset(0, 50, 0, 20);
}

public static Toast showDailog(String textToDisplay){
final Toast toast = new Toast(textToDisplay);
// Display the window.
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
toast.setVisible(true);
}
});
thread.start();
return toast;
}

@Override
public void hide(){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
setVisible(false);
dispose();
}
});
}

public static void main(String... args){
Toast toast = Toast.showDailog("Display");
try{
Thread.sleep(5000);
}
catch (Exception e){}
toast.hide();
}

}

可能会有一些实现上的错误,但这是基本的。

这很好用。但是,当我尝试以资源密集型操作的方式进行设置时,它会跳闸。由于 GIF 动画没有显示,我认为这意味着它有点停滞了。

用途是:

  Toast toast = Toast.showDailog("Generating PDF");

//resource intensive operation. Takes about 3-5seconds to execute

toast.hide();

更让我痛苦的是,即使在处理完“Toast”之后,应用程序也变得非常慢。我很确定速度变慢不是因为有问题的操作,因为如果我取消“ toast ”,它会完美地工作。

有人可以指出这里有什么问题吗???

我经历了this question .但是那里的东西比我要找的要复杂得多。我正在寻找的是一个简单的对话框。不是一个完整的框架,需要容纳多个组件。

最佳答案

试试这段代码:

public class Test extends JFrame {

private JPanel contentPane;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JButton btnTestToast = new JButton("Test Toast");
btnTestToast.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ToastMessage toastMessage = new ToastMessage("Sample text to toast ",3000);
toastMessage.setVisible(true);
}
});
contentPane.add(btnTestToast, BorderLayout.SOUTH);
}

}

public class ToastMessage extends JDialog {
int miliseconds;
public ToastMessage(String toastString, int time) {
this.miliseconds = time;
setUndecorated(true);
getContentPane().setLayout(new BorderLayout(0, 0));

JPanel panel = new JPanel();
panel.setBackground(Color.GRAY);
panel.setBorder(new LineBorder(Color.LIGHT_GRAY, 2));
getContentPane().add(panel, BorderLayout.CENTER);

JLabel toastLabel = new JLabel("");
toastLabel.setText(toastString);
toastLabel.setFont(new Font("Dialog", Font.BOLD, 12));
toastLabel.setForeground(Color.WHITE);

setBounds(100, 100, toastLabel.getPreferredSize().width+20, 31);


setAlwaysOnTop(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int y = dim.height/2-getSize().height/2;
int half = y/2;
setLocation(dim.width/2-getSize().width/2, y+half);
panel.add(toastLabel);
setVisible(false);

new Thread(){
public void run() {
try {
Thread.sleep(miliseconds);
dispose();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
}

关于java - Android 喜欢 Toast in Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10161149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com